Django 1.7 ImageField表单验证 [英] Django 1.7 ImageField form validation

查看:129
本文介绍了Django 1.7 ImageField表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.7与Python 3.4进行单元测试。当 file_data 元素被注释掉时,下面的表单会正常验证。包含 file_data 不会验证,测试失败。

 从django.core.files.uploadedfile导入SimpleUploadedFile 
...

data = {
'phone_number':'123456',
'job':Job .objects.latest('id')。pk,
}
file_data = {
'portrait':SimpleUploadedFile(
content = b'',
content_type = 'image / jpeg',
name ='test.jpg',

}
form = PersonForm(data,file_data)
self.assertTrue(form。 is_valid())

类似的代码适用于 FileField 在我网站的其他地方上传测试。在shell中运行代码我在 form.errors 中得到以下内容:'上传有效的图像。您上传的文件不是图像或损坏的图像。因此,我认为问题在于内容 content_type 字段。我尝试使用像此答案中的图像一字符串无效。 SimpleUloadedFile源代码



该表单在现实中工作正常,但我想确保它被工作覆盖测试将来的维护。理想情况下,我想避免因为图像在我的 .gitignore 中而存在的实际 test.jpg 文件,我不想开始骇客目前一个非常顺利的自动化部署。



我应该输入什么输入 SimpleUploadedFile 使其正确验证?

解决方案

运行 / code> lib你的测试套件和你的工作代码?可以正确使用G $ C图像 lib进程文件如GIF吗?您可能需要检查 PIL /枕头安装。

  SimpleUploadedFile(name ='foo.gif',
content = b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc, \x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00')

适用于我。



表单内部依赖于 form.ImageField to_python()来测试图像,方式如下:

 导入io 
从django.utils.image导入图像
Image.open(io.BytesIO(image_content))。verify()

verify()行的任何异常都会导致错误上传有效的图片...。您可以通过将图像分配给 image_content 来检查图像字节:

  image_content = b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00\x00\x00\x00\x01 \x00\x01\x00\x00\x02\x02D\x01\x00'


$ b $



此外,您可以在线生成图像字节:

  import io 
from django.utils.image import Image
fp = io.BytesIO()
Image.new('P',(1,1)) .save(fp,'png')
fp.seek(0)
portrait = SimpleUploadedFile(name =''foo,content = fp.read())


I'm writing unit tests using Django 1.7 with Python 3.4. The form below validates fine when the file_data element is commented out. With the file_data included it doesn't validate and the test fails.

from django.core.files.uploadedfile import SimpleUploadedFile
...

data = {
    'phone_number': '123456',
    'job': Job.objects.latest('id').pk,
}
file_data = {
    'portrait': SimpleUploadedFile(
        content=b'',
        content_type='image/jpeg',
        name='test.jpg',
    )
}
form = PersonForm(data, file_data)
self.assertTrue(form.is_valid())

Similar code works fine for FileField upload tests elsewhere in my site. Running the code in a shell I get the following in form.errors: 'Upload a valid image. The file you uploaded was either not an image or a corrupted image.' I therefore think the problem lies either in the content or content_type fields. I've tried using an image-as-a-string as in this answer to no avail. [Edit: the image-as-a-string approach was in fact the answer but I must've implemented it badly. The accepted answer is verified as working.] I couldn't find any clues in the SimpleUloadedFile source code.

The form works fine in reality but I want to make sure it's covered by a working test for future maintenance. Ideally I'd like to avoid having to have an actual test.jpg file existing because images are in my .gitignore file and I don't want to have to start hacking what is currently a very smooth automated deployment.

What input should I give SimpleUploadedFile so that it validates correctly?

解决方案

Is there any difference of running Image lib between your test suit and your working code? Can the Image lib process files such as GIF properly? You may need to check the PIL/pillow installation as well.

SimpleUploadedFile(name='foo.gif', 
                   content=b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00')

works for me.

The form internally relies on forms.ImageField to test image in its to_python(), in the way like:

import io
from django.utils.image import Image
Image.open(io.BytesIO(image_content)).verify()

Any exception from the verify() line would result in the error 'Upload a valid image...'. You could check the image bytes by assigning it to image_content:

image_content = b'GIF87a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00ccc,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00'

works for me either.

Also, you could generate image bytes online:

import io    
from django.utils.image import Image
fp = io.BytesIO()
Image.new('P', (1,1)).save(fp, 'png')
fp.seek(0)
portrait = SimpleUploadedFile(name=''foo, content=fp.read())

这篇关于Django 1.7 ImageField表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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