django 'InMemoryUploadedFile' object has no attribute 'field'
I'm having a problem uploading files on my production server. I'm using the django-stdimage on a model as follows:
class League(models.Model):
name = models.CharField(max_length=100)
logo = StdImageField(upload_to='images/league_logos', blank=True, size=(220, 120))
Using the admin app, I can upload templates on my local machine (using the dev server) just fine. On production, I have nginx for static file retrieval and apache with mod_wsgi for the rest. When trying to upload on production, I get the
'InMemoryUploadedFile' o开发者_StackOverflow中文版bject has no attribute 'field'
error. I'm tailing the error log in apache and nothing seems to happen when I try to upload. The error is happening at
site-packages/django/contrib/admin/templates/admin/includes/fieldset.html, error at line 19
which is just a template tag accessing the field.field:
{{ field.field }}
I'm not sure how to go about debugging this. Any suggestions?
Thanks
I found that to be due to PIL not getting installed when I pip installed my requirements. That was a very frustrating issue since the error message didn't really clue me into that.
Luckily I came across this thread:
Why can't I upload jpg files to my Django app via admin/?
which clued me in.
You probably want to pass it a filesystem storage object - then it knows how to convert the InMemoryUploadedFile object that it gets from your browser to an ImageFieldFile, so it can save the image.
# Near top of your file:
from django.core.files.storage import FileSystemStorage
fs = FileSystemStorage(location=settings.MEDIA_ROOT)
# Then in your model:
logo = StdImageField(storage = fs, upload_to='images/league_logos', blank=True, size=(220, 120))
That should save uploaded files to MEDIA_ROOT/images/league_logos.
精彩评论