1024programmer Java Getting error from client post method in django test

Getting error from client post method in django test

Is there a quick way to get errors from form submission when using Client in django tests?

This is a code snippet:

with open(good_path) as fp:
             data = {
                 'account': account_id,
                 'date': date,
                 'file': fp
             respOnse= client.post(reverse('myapp:form-page'), data)

The page is not redirecting properly (response = 200) and I can see that response.content returns an error.

Is there a quick way to isolate errors? I would like to have something similar to response.errors, similar to form instances.

1> Daniel Rosem..:

Assuming that the underlying view uses a template to render its response, you can access that template’s context using response.context. So, for example, if your view does the following:

form = MyForm(request.POST)
 if form.is_valid():
     return redirect(...)
 return render(request, 'template.html', {'form': form})

Your tests can access response.context['form'].errors to view form errors.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/772502

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top