Reputation: 13
I've already created a basic CMS and my next step is to add image upload functionality. I've added some lines to my models.py and after that my model is not validating because of UnicodeDecodeError:
Unhandled exception in thread started by Traceback (most recent call last): File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 93, in w rapper fn(*args, **kwargs) File "C:\Python27\lib\site-packages\django\core\management\commands\runserver. py", line 101, in inner_run self.validate(display_num_errors=True) File "C:\Python27\lib\site-packages\django\core\management\base.py", line 310, in validate num_errors = get_validation_errors(s, app) File "C:\Python27\lib\site-packages\django\core\management\validation.py", lin e 113, in get_validation_errors from django.utils.image import Image File "C:\Python27\lib\site-packages\django\utils\image.py", line 154, in Image, _imaging, ImageFile = _detect_image_library() File "C:\Python27\lib\site-packages\django\utils\image.py", line 134, in _dete ct_image_library "imported: %s") % err File "C:\Python27\lib\site-packages\django\utils\functional.py", line 168, in __mod__ return six.text_type(self) % rhs UnicodeDecodeError: 'ascii' codec can't decode byte 0xb3 in position 35: ordinal not in range(128)
Here is my models.py code:
from django.db import models from django.contrib.auth.models import User ... class Photo(models.Model): title = models.CharField(max_length=255) upload_path = '/' photo = models.ImageField(upload_to=upload_path) def __unicode__(self): return self.title
I've got Python 2.7.6, Django 1.6.1, MySQL-python-1.2.3.
Does anybody know why the exception occurs?
Upvotes: 0
Views: 1808
Reputation: 5656
Your problem could be that your unicode method is not really returning unicode.
It should be something like
def __unicode__(self):
return u'%s' % self.title
If you say that this did not fix your error, you are correct. I went looking in django code and saw that the error is generated elsewhere. Still, your unicode methods should return unicode not anything else.
Now about the error: It is raised where django tries to import your imaging library. From what i see, the error appears when raising error generates an error.
That you cant fix, but what you can do is to check out if you have required imaging libraries installed. The code that fails ( and generates original error ) is:
import _imaging as PIL_imaging
How you can fix it (most likely) is to remove PIL from your computer (if you even have it) and install PILLOW. If you read through the 1.6 release notes : https://docs.djangoproject.com/en/dev/releases/1.6/ you can see, that pillow is now the preferred image manipulation library for Django.
Copy from linked page: Pillow is now the preferred image manipulation library to use with Django. PIL is pending deprecation (support to be removed in Django 1.8). To upgrade, you should first uninstall PIL, then install Pillow.
Go there, follow instructions for uninstalling PIL and installing PILLOW and try your code again.
Edit You actually do not need to uninstall/remove PIL. Removing it could cause problems with stuff like ubuntu, where PIL is apparently needed for gnome3 desktop...
/Edit
Upvotes: 1
Reputation: 13
Thank you very much, Odif Yltsaeb! It works!
Instruction for newbies like me:
1) Go to https://pypi.python.org/pypi/Pillow/2.3.0 and download proper version of Pillow
2) To your models.py add
import PIL.Image as Image
Now it should work!
Upvotes: 1