将ImageField添加到模型会导致django中的异常 [英] Adding ImageField to model causes exception in django

查看:394
本文介绍了将ImageField添加到模型会导致django中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个基本的CMS,我的下一步是添加图像上传功能。我已经在我的models.py中添加了一些行,之后我的模型由于UnicodeDecodeError而无法验证:

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)

这是我的models.py代码:

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?

推荐答案

你的问题可能是你的unicode方法没有真正返回unicode。

Your problem could be that your unicode method is not really returning unicode.

应该是像

def __unicode__(self):
    return u'%s' % self.title

如果你说没有解决你的错误,你是正确的。我去查找django代码,看到错误是在其他地方生成的。然而,你的unicode方法应该返回unicode不是其他任何东西。

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.

现在关于错误:
在django尝试导入您的成像库时,它被提升。从我看到的,当提高错误产生错误时出现错误。

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

如何解决它(很有可能)是从计算机中删除PIL(如果你有),然后安装PILLOW。阅读1.6发行说明:
https://docs.djangoproject .com / en / dev / releases / 1.6 / 你可以看到,这个枕头现在是Django的首选图像处理库。

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.

从链接的页面复制枕头现在是Django使用的首选图像处理库。 PIL正在等待弃用(支持在Django 1.8中删除)。要升级,您应该首先卸载PIL,然后安装枕头。

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.

按照说明卸载PIL并安装PILLOW,然后再次尝试代码。

Go there, follow instructions for uninstalling PIL and installing PILLOW and try your code again.

修改
您实际上不需要卸载/删除PIL。删除它可能会导致像ubuntu这样的东西的问题,其中PIL显然需要gnome3桌面...

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...

/编辑

这篇关于将ImageField添加到模型会导致django中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆