gpt4 book ai didi

django - 在 Django 中测试表单 View 时指定提交按钮

转载 作者:行者123 更新时间:2023-12-05 02:07:41 25 4
gpt4 key购买 nike

我正在尝试测试 Django 表单 View :

测试.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

但是我的 View 会检查使用哪个按钮来提交表单:

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 - 在 Django 中测试表单 View 时指定提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61531393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com