I'm attempting to open a thumbnail of an image that was saved with django-versatileimagefield
https://django-versatileimagefield.readthedocs.io/en/latest/
file = instance.image.thumbnail['1920x1080'].open(mode='rb')
I'm getting the following error:
'SizedImageInstance' object has no attribute 'open'
I have success with the following line of code, but I want to open the smaller version as opposed to the original version
file = instance.image.open(mode='rb')
If it helps, the url of the image is instance.image.thumbnail['1920x1080'].url
Thanks!
Related
I am trying to upload image from the folder on my PC, and the problem is- I cannot use send_keys as it should be uploaded from the list of images from the folder. Please see attached photo and code:
def picture(self):
path = "/home/nataliya/Desktop/dog.png"
editPhoto = self.driver.find_element_by_xpath('//* [#id="easSettingAvatarUpload"]')
editPhoto.click()
time.sleep(2)
editPhoto.send_keys(path)
editPhoto.submit()
Error I get: AttributeError: 'NoneType' object has no attribute 'send_keys'
I was calling a wrong thing :)
That worked:
path = "/home/nataliya/Desktop/cat.jpg"
editPhoto = self.driver.find_element_by_xpath('//*[#id="easSettingAvatarUpload"]')
time.sleep(2)
editPhoto.send_keys(path)
time.sleep(2)
I cannot seem to create and attach a file to an item in podio using the pypodio client, which is the python wrapper for PODIO's API. I am trying to get the file id but keep on getting the error. I am using Python 3.6.0
My code is
`path = os.getcwd()`
`filename="system_information"`
`filepath = path + "\\system_information.txt"`
`filedata=open(filepath)`
uploading_response = pcbapp.Files.create(filename,filedata)
I get an error shown below,
File "c:\users\nipun.arora\src\podio-py\pypodio2\encode.py", line 317, in get_headers
boundary = urllib.quote_plus(boundary)
AttributeError: module 'urllib' has no attribute 'quote_plus'
That's might be because there is no urllib.quote_plus in python3. Can you try running same code in python2?
I have a DjangoFileField in my model. I am trying to convert the type of the audio from that FielField to mp3 and then again trying to save it. But after converting the type and exporting it using pydub it is returning the following error
AttributeError: 'file' object has no attribute '_committed'
My code is like this
def get_from_function(AudioSegment, format):
form = "from_{0}".format(format)
print form
if hasattr(AudioSegment, form):
return getattr(AudioSegment, form)
return None
audio = request.FILES.get('audio', None)
if audio:
name_list = audio.name.rsplit(".")
voice_format =name_list[1]
from_format = get_from_function(AudioSegment, voice_format)
if from_format and callable(from_format):
sound = from_format(audio)
audio = sound.export("media/{0}".format(name_list[0]), mp3")
when i print the audio it prints
<open file 'media/barsandtone', mode 'wb+' at 0x7f771e5e2f60>
and when i print the type of file it prints
<type 'file'>
but when i assign the audio field to django model like
Mymodel.objects.create(audio=audio)
it gives error
AttributeError at /create/
'file' object has no attribute '_committed'
What is the correct way to save the exported file into django model
django needs a ContentFile usually to do this passing it a stream of data, and it doesn't work the way you usually pass arguments to a model. The proper way to do this is the following
from django.core.files.base import ContentFile
[...]
mymodel = Mymodel()
mymodel.audio.save(audio.name, ContentFile(audio.read()))
don't forget to pass the stream to ContentFile. Django won't raise any errors if you pass it ContentFile(audio) but in that case you won't save the file content..
I got same error that is solved now i would suggest you to open that file in read mode then save it. here is my code :
from django.core.files import File as DjangoFile
file_obj1 = DjangoFile(open(file_name, mode='rb'), name=file_name)
File.objects.create(title=file_name, file=file_obj1, content_object=task_obj, author=task_obj.client)
In my case I was creating a django form with some FileFields for which I wanted to show the currently existing file from a model's instance. I'm leaving this answer here in case anyone else end up here for similar reasons, even though I know that this answer doesn't apply to the OP's situation.
Normally you would bound the form and the model but in this case it's not possible for reasons a bit long to explain, so I wanted to query the model to obtain the File field and make it be the initial data.
It reached a point where I managed to make it work until the view can render the field and the currently existing field (as it's normally rendered by the ClearableFileInput widget).
That point was reached by filling self.fields["field_name"].initial with either:
Using a DummyFile class:
class DummyFile:
def __init__(self, url, name):
self.url = url
self.name = name
def __str__(self):
return self.name
Filling it with the existing instance, like
self.fields[field_name].initial = doc.file.instance
self.fields[field_name].initial.url = doc.file.url
But then in both cases the AttributeError: 'file' object has no attribute '_committed' was raised when uploading a file, although it was correctly uploaded and saved.
Solution
Turned out that it behaves different when you assign the initial data for a field by using self.fields[field_name].initial than using self.initial[field_name], and doing it like this worked like a charm:
doc = Document.objects.filter(
filters_to_locate_the_model_instance=this_and_that,
).first()
if doc:
self.initial[field_name] = doc.file
The HTML is rendered with the link to the existing file, it loads fine if there is not an existing file, and uploads and saves a file without any error.
I receive an error
File.open(classname+'.txt','a')
AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
while trying to open a file. I need to open the file and write to the file with the scores.
Here is the code
if Exists==False:
File.open(classname+'.txt','a')
File.write(name+','+surname+','+str(1)+','+str(score)+'/n')
else:
File=open(classname+'.txt','w')
linecount=len(filelines)
for i in range(0,linecount):
File.write(filelines[i])
it should be
File=open(classname+'.txt','a')
File.write(name+','+surname+','+str(1)+','+str(score)+'/n')
File.close()
The problem is that at the beginning you declare
File=open(classname+'.txt','r+')
and then you ask again to open File
File.open(classname+'.txt','a')
but File is already open(classname+'.txt','r+'). Just skip File.open(classname+'.txt','a') and it should work fine.
I am trying to read image from url and save it to model. but i am getting this error message:
AttributeError: addinfourl instance has no attribute '__committed'
this is my code:
location = Location()
location.save()
image = urllib.urlopen(img_url)
location.locations_image.create(bild=image)
I guess, i am doing something wrong while reading an image file from url. can someone pls guide me? thanks a lot