Django如何确定上传的图片是否有效? [英] How does Django determine if an uploaded image is valid?

查看:417
本文介绍了Django如何确定上传的图片是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



models.py



<$
=$
这是产品的主图像
product = models.ForeignKey(Product)
photo = models.ImageField(upload_to ='products')

在开发模式下,尝试通过Django管理员上传图像,我不断得到:

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

我试图上传的jpg可以使用os X Preview查看,所以似乎是



似乎问题是Python Imaging Library不会将其识别为图像。为什么会出现一个有效的图像?



PIL已安装,通过Django shell验证。

解决方案

根据Django的源代码。这三行负责验证图像:

 从PIL导入图像
trial_image = Image.open(file)
trial_image.verify()

PIL可能不支持图像类型。检查支持的格式列表此处


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

models.py

class ImageMain(models.Model):
"""This is the Main Image of the product"""
    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:

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

The jpg I'm trying to upload can be viewed with os X Preview so it would seem to be valid.

It seems the problem is Python Imaging Library doesn't recognize it as an image. Why would that be happening with a valid image?

PIL is installed, verified via the Django shell.

解决方案

According to Django's source code. Those three lines are responsible for verifying images:

from PIL import Image
trial_image = Image.open(file)
trial_image.verify()

The image type could be unsupported by PIL. Check the list of supported formats here

这篇关于Django如何确定上传的图片是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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