作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在编写一个测试来检查我的表单中是否出现了验证错误。
在这样做时我收到以下错误:
======================================================================
FAIL: test_start_date_before_end_date_errors (reports.tests.ScheduleValidation)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/jwe/piesup2/reports/tests.py", line 61, in test_start_date_before_end_date_errors
self.assertFormError(response, 'form', None, 'End Date Must Be Greater Than Start Date')
File "/home/jwe/piesup2/venv/lib/python3.4/site-packages/django/test/testcases.py", line 467, in assertFormError
" response" % form)
AssertionError: The form 'form' was not used to render the response
----------------------------------------------------------------------
我专门检查的验证错误是在 clean()
方法内的 models.py 中引发的
class Schedule(models.Model)
...
...
def clean(self):
if self.start_date and self.end_date:
# Ensure that the end_date occurs after the start_date
if self.end_date <= self.start_date:
err = "End Date Must Be Greater Than Start Date"
raise ValidationError(err)
我正在测试的通用 CreateView 如下所示:
class ScheduleCreate(SuccessMessageMixin, FetchURLMixin, CreateView):
model = Schedule
form_class = ScheduleCreateForm
template_name_suffix = '_create_form'
我能够将 View 模板中的错误呈现为 non_field_errors
,如下所示:
{% for error in form.non_field_errors %}
<label>{{ error }}</label>
{% endfor %}
我的测试如下所示:
class ScheduleValidation(RequiresLogin, TestCase):
def setUp(self):
self._client = client_fixture.create()[0]
# Setup some example sample data for a Schedule
self.data = {
'client': self._client.id,
'schedule_date': today,
'billing_period': 'Q',
'schedule_type': 'SE',
}
def test_start_date_before_end_date_errors(self):
self.data['start_date'] = today
self.data['end_date'] = yesterday
response = self.client.post('reports:schedule-create', self.data, follow=True)
# Check if redirects back to the page
with self.assertTemplateUsed('schedule_create_form.html'):
self.assertFormError(response, 'form', None, 'End Date Must Be Greater Than Start Date')
我将字段类型设置为 None
以访问 non_field_errors
,详见文档 here
在我的 View 模板中,我可以使用 {{ form }}
引用表单,包括任何 non_field_errors
。这意味着它已正确传递给模板。
我可以检查 View 本身内部的上下文数据。
def get_context_data(self, **kwargs):
context = super(ScheduleCreate, self).get_context_data(**kwargs)
assert False
return context
从这里的断点开始,我可以在浏览器中使用 Werkzeug 记录上下文变量的内容,这表明“表单”实际上已传递给模板
[console ready]
>>> context
{'form': <ScheduleCreateForm bound=True, valid=False, fields=(client;schedule_date;start_date;end_date;billing_period;schedule_type)>, 'view': <reports.views.ScheduleCreate object at 0x7f256eb6c908>}
这引出了一个问题,为什么我会收到此错误,我该如何解决?
最佳答案
当您使用 client.post()
时,您应该使用实际的 URL,而不是 url 的名称。
您可以对其进行硬编码,例如:
response = self.client.post('/reports/schedules/create/, self.data, follow=True)
或者你可以反转 url:
from django.core.urlresolvers import reverse
url = reverse('reports:schedule-create')
response = self.client.post(url, self.data, follow=True)
关于python - Django 测试 : AssertionError: The form 'form' was not used to render the response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38566704/
我是一名优秀的程序员,十分优秀!