在 Django 中测试表单视图时指定提交按钮
specify submit button when testing a form view in Django
我正在尝试测试 Django 表单视图:
tests.py
form_data = {
'comment_text': "test comment"
}
response = self.client.post(reverse('announcements:comment', args=[self.test_announcement.id]), form_data)
self.assertEqual(response.status_code, 404) # invalid submit button
但我的视图会检查使用哪个按钮提交表单:
views.py
def my_view(request, announcement_id):
# ...
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
# ...
if 'comment_button' in request.POST:
# how do I get inside this branch from a test?
# process form
else:
raise Http404
如何模仿 'comment_button' 在我的测试中用于提交表单?
将 comment_button
字段添加到 form_data
form_data = {
'comment_text': "test comment",
'comment_button': True
}
我正在尝试测试 Django 表单视图:
tests.py
form_data = {
'comment_text': "test comment"
}
response = self.client.post(reverse('announcements:comment', args=[self.test_announcement.id]), form_data)
self.assertEqual(response.status_code, 404) # invalid submit button
但我的视图会检查使用哪个按钮提交表单:
views.py
def my_view(request, announcement_id):
# ...
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
# ...
if 'comment_button' in request.POST:
# how do I get inside this branch from a test?
# process form
else:
raise Http404
如何模仿 'comment_button' 在我的测试中用于提交表单?
将 comment_button
字段添加到 form_data
form_data = {
'comment_text': "test comment",
'comment_button': True
}