I now work with mongoengine, mongoForm and set this in setting.py DEFAULT_FILE_STORAGE = 'mongoengine.django.storage.GridFSStorage'
I need to upload a picture, and I wrote a form like this: from django.forms import ImageField from mongoforms import MongoForm from documents import UserProfile class ProfileForm(MongoForm): photo = ImageField() class Meta: document = UserProfile exclude = ('user', 'photo') but I get a Invalid Image Error when I submit a picture( how ever, it's ok when I use PIL.Image.open(photo).verify() ) I review the code, and found the exception was raise on django.forms.fields.ImageField.to_python method code snippet for to_python method: def to_python(self, data): .... if hasattr(data, 'temporary_file_path'): file = data.temporary_file_path() .... try: Image.open(file).verify() except ImportError: raise except Exception: # Python Imaging Library doesn't recognize it as an image raise ValidationError(self.error_messages['invalid_image']) ... well, I don't know well to set temporary_file_path and how to storage it with mongoengine.django.storage.GridFSStorage # this was snippet for class ImageField def to_python(self, data): """ Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports). """ f = super(ImageField, self).to_python(data) if f is None: return None # Try to import PIL in either of the two ways it can end up installed. try: from PIL import Image except ImportError: import Image # We need to get a file object for PIL. We might have a path or we might # have to read the data into memory. if hasattr(data, 'temporary_file_path'): file = data.temporary_file_path() else: if hasattr(data, 'read'): file = StringIO(data.read()) else: file = StringIO(data['content']) try: # load() could spot a truncated JPEG, but it loads the entire # image in memory, which is a DoS vector. See #3848 and #18520. # verify() must be called immediately after the constructor. Image.open(file).verify() except ImportError: # Under PyPy, it is possible to import PIL. However, the underlying # _imaging C module isn't available, so an ImportError will be # raised. Catch and re-raise. raise except Exception: # Python Imaging Library doesn't recognize it as an image raise ValidationError(self.error_messages['invalid_image']) if hasattr(f, 'seek') and callable(f.seek): f.seek(0) return f this was exception stack Environment: Request Method: POST Request URL: http://localhost:8000/accounts/profile/ Django Version: 1.4 Python Version: 2.7.3 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'registration', 'bussiness', 'accounts', 'debug_toolbar'] Installed Middleware: ['django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'debug_toolbar.middleware.DebugToolbarMiddleware'] Traceback: File "/home/yan/env/haoyoubang/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/home/yan/env/haoyoubang/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 20. return view_func(request, *args, **kwargs) File "/home/yan/git/haoyoubang/accounts/views.py" in profile 31. processFile(request.FILES['photo']) File "/home/yan/git/haoyoubang/accounts/views.py" in processFile 21. s = f.clean(photo) File "/home/yan/env/haoyoubang/local/lib/python2.7/site-packages/django/forms/fields.py" in clean 535. return super(FileField, self).clean(data) File "/home/yan/env/haoyoubang/local/lib/python2.7/site-packages/django/forms/fields.py" in clean 153. value = self.to_python(value) File "/home/yan/env/haoyoubang/local/lib/python2.7/site-packages/django/forms/fields.py" in to_python 593. raise ValidationError(self.error_messages['invalid_image']) Exception Type: ValidationError at /accounts/profile/ Exception Value: [u'\u8bf7\u4e0a\u4f20\u4e00\u5f20\u6709\u6548\u7684\u56fe\u7247\u3002\u60a8\u6240\u4e0a\u4f20\u7684\u6587\u4ef6\u4e0d\u662f\u56fe\u7247\u6216\u8005\u662f\u5df2\u635f\u574f\u7684\u56fe\u7247\u3002'] thank you !! -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/1ZkKiljeFHcJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.