Django 1.7 ImageField form validation

前提是你 提交于 2019-12-22 09:32:59

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/26141786/django-1-7-imagefield-form-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!