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.