从django测试中的客户端post方法获取错误
在django测试中使用客户机时,是否有一种快速方法可以从表单提交中获取错误 下面是一段代码片段:从django测试中的客户端post方法获取错误,django,testing,post,Django,Testing,Post,在django测试中使用客户机时,是否有一种快速方法可以从表单提交中获取错误 下面是一段代码片段: with open(good_path) as fp: data = { 'account': account_id, 'date': date, 'file': fp } response = client.post(revers
with open(good_path) as fp:
data = {
'account': account_id,
'date': date,
'file': fp
}
response = client.post(reverse('myapp:form-page'), data)
页面重定向不正确(response=200),我可以在response.content
中看到返回了一个错误
有没有一种快速的方法来隔离错误?我希望类似于表单实例的
response.errors
。假设基础视图使用模板呈现其响应,您可以使用response.context
访问该模板的上下文。例如,如果您的视图执行以下操作:
form = MyForm(request.POST)
if form.is_valid():
return redirect(...)
return render(request, 'template.html', {'form': form})
您的测试可以访问
response.context['form'].errors
以查看表单错误。将视图添加到发布数据的问题中