0

I have problem testing form, it wont accept file upload even I tried many ways, it just returns error that file upload field can't be empty:

AssertionError: False is not true : <ul class="errorlist"><li>file<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

Test.py:

def test_applicant_form(self):
        placements = Position.objects.filter(published=True)[0]
        client = Client()
        file =  File(open('root/static/applications/file.pdf', 'rb'))
        data_set = {'name': 'Caleb', 'surname': 'Dvorszky',
            'phone_number': '+1963124575', 'city': 'Kansas City', 'country': 'United States', 'message': 'Would like to be there', 'file':file}
        form = ApplicantForm(data=data_set)
        self.assertTrue(form.is_valid(), form.errors)

even tried and:

response = client.post(placements.get_absolute_url,data=data_set, content_type='multipart/form-data')

And still doesnt work.

Here is forms.py

class ApplicantForm(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control form-control-lg'}))
    surname = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control form-control-lg'}))
    phone_number = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control form-control-lg'}))
    city = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control form-control-lg'}))
    country = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control form-control-lg'}))
    message = forms.CharField(widget=forms.Textarea(attrs={'class': 'form-control form-control-lg'}))
    file = forms.FileField(widget=forms.FileInput())

    class Meta:
        model = Candidate
        exclude = ['position', 'seen']

models.py This is model that need to be saved when form is filled with data

class Applicant(models.Model):
    date = models.DateTimeField(auto_now_add=True)
    name = models.CharField(max_length=100)
    surname = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    country = models.CharField(max_length=100)
    position = models.ForeignKey(
        Position, on_delete=models.CASCADE,
        related_name='applicants')
    cover_letter = models.TextField()
    file = models.FileField(upload_to='files/applications')
    seen = models.BooleanField(default=False, editable=False)

    ADMIN_DISPLAY = ['get_name', 'position', 'city', 'country', 'get_file', 'date']

    def get_name(self):
        return "%s %s" % (self.name, self.surname)
    get_name.short_description = 'Name'

    def get_file(self):
        return '<a href="%s%s" target="_blank">Download</a>' % (
            settings.BASE_DOMAIN_URL, self.file.url)
    get_file.allow_tags = True
    get_file.short_description = 'Download file'

And here is views.py , just form part:

if request.method == 'POST':
        form = ApplicantForm(request.POST, request.FILES)
        if form.is_valid():
            applicant = form.save(commit=False)
            applicant.position = placement
            applicant.save()
            notification(applicant)
            messages.info(request, 'We received your application. See you soon!')
            return redirect('postions:position-post', placement.slug)
    form = ApplicantForm()

1 Answer 1

1

You seem to not have passed in the file properly. Also you seem to be trying to access a data keyword argument you didn't define in your form. Try out this: Remember to import `SimpleUploadedFile from django.core.files.uploadedfile

  from django.core.files.uploadedfile import SimpleUploadedFile

  def test_applicant_form(self):
    placements = Position.objects.filter(published=True)[0]
    with open('root/static/applications/file.pdf', 'rb') as file:
      document = SimpleUploadedFile(file.name, file.read(), content_type='application/pdf')
    data_set = {'name': 'Caleb',
    'surname':'Dvorszky',
    'phone_number': '+1963124575',
    'city': 'Kansas City',
    'country':'United States',
    'message': 'Would like to be there',
    }
    form = ApplicantForm(data_set, {'file': document})
    self.assertTrue(form.is_valid(), form.errors)

Look at this link on testing for further reading. You could also look at this link on testing file upload with django

Sign up to request clarification or add additional context in comments.

2 Comments

I jos got same error again with this AssertionError: False is not true : <ul class="errorlist"><li>file<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
@havoc I've edited the answer to upload a test image file. It works on my local machine. change the content_type keyword argument to 'application/pdf'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.