Friday, September 1, 2017

Re: Testing file upload via a form?

Thanks James, I will try that.

On 1 September 2017 at 09:27, James Schneider <jrschneider83@gmail.com> wrote:


On Aug 29, 2017 11:49 AM, "Derek" <gamesbook@gmail.com> wrote:
(Python 3.5 and Django 1.10)

I am trying to test a Django form that enables a required file upload.

The form is very simple and includes a field that looks like:

    upload_file = forms.FileField()

The corresponding test to try and check the upload:

def test_form_validation_with_file(self):
    fake_file = ContentFile(b'''Some file content''')
    fake_file.name = 'fake.xls'
    suf_file = SimpleUploadedFile('suf.xls', b'''this is text''')
    data = {
        'upload_file': suf_file,
    }
    form = forms.RequestForm(data=data)
    self.assertTrue(form.is_valid())

The form validation fails i.e. it does not seem to recognise that file data has been suppplied.

I have also tried with the  "fake_file" for the data but without success.

Any help or pointers appreciated.

It does not appear that you are binding the file to the form correctly. Django forms use a separate argument to the form class for file content. It is not part of the form 'data'.


-James

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/GUStj-3rzD8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWEKPqL7%3DLpVpREaZExSediHf6nqa5mrbWoynfi8jYy5g%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF1Wu3Md5D%2B-fg%2BQZE5DEHyyqV0qGkiq7i%3DUHiv1QGi86oNX0Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment