Why can't I upload jpg files to my Django app via admin/?

后端 未结 9 1930
借酒劲吻你
借酒劲吻你 2020-12-16 14:16

I\'m trying to add images to my models in my Django app.

models.py

class ImageMain(models.Model):
  product = models.ForeignKey(Product)
  photo = mo         


        
相关标签:
9条回答
  • 2020-12-16 14:21

    Django is trying to import PIL from a folder called PIL, but PIL installs itself in a folder called e.g. PIL-1.1.7-py2.6-macosx-10.6-universal.egg, so the import fails - which Django (or PIL ?) seem to interpret as corrupt image.

    A simple symlink

    host:~ user$ cd /Library/Python/2.6/site-packages
    host:site-packages user$ ln -vis PIL-1.1.7-py2.6-macosx-10.6-universal.egg PIL
    create symbolic link `PIL' to `PIL-1.1.7-py2.6-macosx-10.6-universal.egg'
    

    has fixed this for me on a Mac OSX 10.6.x MBP. On Linux machines, the folder might instead be called dist-packages and be underneath /usr/lib/python/ or so, but the idea is the same.

    You can often find the folder where python modules are usually installed to as described here.

    0 讨论(0)
  • 2020-12-16 14:24

    Note, for anyone getting this error with a virtualenv on Ubuntu, this post is helpful.

    Basically, Ubuntu installs shared objects to locations that pip doesn't know about. In this case, pip expects system libraries to be in /usr/lib, but Ubuntu puts them in /usr/lib/x86_64-linux-gnu, or some other architexture-dependent location.

    The short-term fix is to simply symlink the libraries into /usr/lib:

    sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib/
    sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/
    sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/
    

    This fixed the error for me on Ubuntu 12.04.

    0 讨论(0)
  • 2020-12-16 14:27

    I met the same problem in Ubuntu server, then you can fix it by installing libjpeg-dev before PIL.

    sudo apt-get install libjpeg-dev
    sudo pip install PIL --upgrade
    

    and if you already installed libjpeg-dev after PIL. then you can remove PIL first, and try to reinstall PIL as following.

    sudo pip uninstall PIL
    sudo apt-get install libjpeg-dev
    sudo pip install PIL
    

    It works for me, and hope it works for you.

    0 讨论(0)
  • 2020-12-16 14:31

    Did you install libjpeg after PIL was already compiled/installed on the system? Maybe it can't find the decoder properly?

    Here's one set of instructions I found on getting libjpeg and PIL playing nicely on MacOS (see towards the end; looks like you may need to explicitly set the dir of the decoder):

    http://djangodays.com/2008/09/03/django-imagefield-validation-error-caused-by-incorrect-pil-installation-on-mac/

    0 讨论(0)
  • 2020-12-16 14:36

    I had a similar problem on Ubuntu 11.04. Apparently in Ubuntu 11, it seems that you need libjpeg8-dev, rather than libjpeg or libjpeg62. Thankyou to Guillaumes post http://gpiot.com/ubuntu-9-10-install-pil-in-virtualenv/

    0 讨论(0)
  • 2020-12-16 14:40

    'python -v' then 'import _imaging' is useful for figuring out where _imaging.so is loaded from. If you reinstall PIL without cleaning out the PIL dir in site-packages you may still be running with an old _imaging.so under the Python package dir. PIL's selftest.py will pass, because you've got a new _imaging.so in your build dir. And make sure you edit JPEG_ROOT in setup.py to pick up the correct header and .so directories at build time.

    0 讨论(0)
提交回复
热议问题