python 为什么我不能通过 admin/ 将 jpg 文件上传到我的 Django 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1402002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:08:19  来源:igfitidea点击:

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

pythondjango

提问by BryanWheelock

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

我正在尝试将图像添加到我的 Django 应用程序中的模型中。

models.py

模型.py

class ImageMain(models.Model):
  product = models.ForeignKey(Product)
  photo = models.ImageField(upload_to='products')

In development mode, every time I try to upload the image via Django admin, I keep getting:

在开发模式下,每次我尝试通过 Django admin 上传图像时,我都会收到:

Upload a valid image. The file you uploaded was either not an image or a corrupted image.

上传有效图片。您上传的文件不是图像或损坏的图像。

I installed libjpeg via fink and then installed PIL 1.1.6 on ox X 10.5.7

我通过 fink 安装了 libjpeg,然后在 ox X 10.5.7 上安装了 PIL 1.1.6

from PIL import Image
file = open('/Users/Bryan/work/review_app/media/lcdtvs/samsung_UN46B6000_front.jpg', 'r')
trial_image = Image.open(file)
trial_image.verify()

It seems that the jpg is valid based on that session. However, it does not load. I have tried other jpgs, they don't work either.

基于该会话,jpg 似乎是有效的。但是,它不会加载。我试过其他 jpg,它们也不起作用。

What could be going wrong?

可能出什么问题了?

I was able to successfully upload a png file.

我能够成功上传一个 png 文件。

采纳答案by pithyless

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

您是否在系统上编译/安装了 PIL 之后安装了 libjpeg?也许它无法正确找到解码器?

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):

这是我发现的一组关于在 MacOS 上很好地播放 libjpeg 和 PIL 的指令(见最后;看起来您可能需要明确设置解码器的目录):

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

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

回答by hyliker

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

我在 Ubuntu 服务器中遇到了同样的问题,然后你可以通过在 PIL 之前安装 libjpeg-dev 来解决它。

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.

如果您已经在 PIL 之后安装了 libjpeg-dev。那么您可以先删除PIL,然后尝试重新安装PIL,如下所示。

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

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

它对我有用,希望对你有用。

回答by Cerin

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

请注意,对于在 Ubuntu 上使用 virtualenv 遇到此错误的任何人,这篇文章很有帮助。

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.

基本上,Ubuntu 将共享对象安装到 pip 不知道的位置。在这种情况下,pip 期望系统库位于 /usr/lib 中,但 Ubuntu 将它们放置在 /usr/lib/x86_64-linux-gnu 或其他一些依赖于架构的位置。

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

短期修复是简单地将库符号链接到 /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.

这为我修复了 Ubuntu 12.04 上的错误。

回答by Rahul

just do
sudo easy_install PIL

只需执行
sudo easy_install PIL

this will install PIL specific to your os. please make sure that the egg file generated in /usr/local/lib/python2.6/dist-packages/ is having egg information (because when i did the same, egg file was not correct). If not then just rename the PIL-build-specific-name to PIL and add a PIL.pth file in the dist-packages folder. write PIL in the PIL.pth file and you are done

这将安装特定于您的操作系统的 PIL。请确保/usr/local/lib/python2.6/dist-packages/中生成的egg文件有egg信息(因为当我这样做时,egg文件不正确)。如果没有,那么只需将 PIL-build-specific-name 重命名为 PIL 并在 dist-packages 文件夹中添加一个 PIL.pth 文件。在 PIL.pth 文件中写入 PIL 就完成了

回答by Neil_Alex

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/

我在 Ubuntu 11.04 上遇到了类似的问题。显然在 Ubuntu 11 中,您似乎需要 libjpeg8-dev,而不是 libjpeg 或 libjpeg62。感谢 Guillaumes 发布http://gpiot.com/ubuntu-9-10-install-pil-in-virtualenv/

回答by Vijesh Venugopal

I had a similar problem and i had cleared it with referring below link:

我有一个类似的问题,我已经通过参考以下链接清除了它:

http://osqa.sjsoft.com/questions/96/why-is-there-no-jpegpng-decoder-when-i-pip-install-pil-on-ubuntu

http://osqa.sjsoft.com/questions/96/why-is-there-no-jpegpng-decoder-when-i-pip-install-pil-on-ubuntu

回答by ssc

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.

Django 试图从名为 的文件夹导入 PIL PIL,但 PIL 将自身安装在名为 eg 的文件夹中PIL-1.1.7-py2.6-macosx-10.6-universal.egg,因此导入失败 - Django(或 PIL?)似乎将其解释为损坏的图像。

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-packagesand be underneath /usr/lib/python/or so, but the idea is the same.

已经在 Mac OSX 10.6.x MBP 上为我解决了这个问题。在 Linux 机器上,该文件夹可能会被调用dist-packages并位于下方/usr/lib/python/左右,但想法是相同的。

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

您通常可以找到通常安装 python 模块的文件夹,如here所述。

回答by osullivj

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

'python -v' 然后 'import _imaging' 对于确定 _imaging.so 的加载位置很有用。如果您在没有清除站点包中的 PIL 目录的情况下重新安装 PIL,您可能仍然在 Python 包目录下使用旧的 _imaging.so 运行。PIL 的 selftest.py 将通过,因为您的构建目录中有一个新的 _imaging.so。并确保在 setup.py 中编辑 JPEG_ROOT 以在构建时选择正确的标题和 .so 目录。

回答by Botond Béres

I had this problem although on Linux not Mac, so might not be able to give too specific info. However you might need libjpeg-devel too (if there is a correspondent for Mac).

我在 Linux 上遇到了这个问题,而不是 Mac,所以可能无法提供太具体的信息。但是,您可能也需要 libjpeg-devel(如果有适用于 Mac 的通讯员)。

Also make sure to purge your current PIL installation completely from the system. And after you are sure libjpeg is installed properly then reinstall PIL with build_ext -i. Then run PIL's selftest.py to check if it gives the JPEG error.

还要确保从系统中完全清除当前的 PIL 安装。在确定 libjpeg 安装正确后,然后使用 build_ext -i 重新安装 PIL。然后运行 ​​PIL 的 selftest.py 来检查它是否给出了 JPEG 错误。