Django - test image loading

Using Photologist, I can upload images as part of the form without any problems. However, in test plans, it’s hard for me to get an image for verification.

In the tests.py file:

data_photo = {'competition': self.newcomp,  
                  'title': 'Rabbit',
                  'image': open('photocompetitions/static/img/body_bg.jpg'),
                  'flickr_id': '425258',
                  'description': 'A picture of a rabbit',
                  'location': 'POINT (5000 5000)',
                  'location_description': 'Just some random place',
                  'copyright': 'Copyright 2011'}
photoform = PhotoForm(data_photo)

Everything works fine, except for the "image" field, which fails because "this field is required." therefore, I assume that it is not accepted, despite the open () command. the β€œimage” field is a photograph of the ImageModel model and appears on the site as a standard upload form.

+3
source share
3 answers

You must pass the image not in data, but in the filesform parameter .

from django.core.files.uploadedfile import SimpleUploadedFile

img = open('photocompetitions/static/img/body_bg.jpg')
uploaded = SimpleUploadedFile(img.name, img.read())
photoform = PhotoForm(data_photo, files={'image': uploaded})
+6
source

"rb" :

'image': open('photocompetitions/static/img/body_bg.jpg', 'rb')
+2

BMP, jpg?

, , PIL django.

The jpg format may not be included correctly in this case, but bmp should be fine.

I hope this helps

0
source

All Articles