gpt4 book ai didi

python - Django testCase 中的 POST 日期值

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:11 25 4
gpt4 key购买 nike

我正在尝试编写一个测试来注册用户。我无法验证表单,因为我不知道如何向表单提交有效日期。这是有问题的代码:

class AccountManagementTest(TestCase):
def test_signup(self):
response = self.client.post(reverse('register'), {
'first_name': 'Gandalf',
'last_name': 'TheGrey',
'email': 'gandalf@me.com',
'password1': 'SauronSucks',
'password2': 'SauronSucks',
'birthdate': datetime(1956, 1, 1),
'tos': True})

之后我可以输出 response.content 并且表单中的错误是“请输入有效日期”。我没有覆盖表单中 birthdateclean 方法。这是表单中日期字段的声明:

birthdate = forms.DateField(
widget=SelectDateWidget(
years=range(this_year-90, this_year-12)[::-1]),
required=True,)

我怎么给它发送一个有效日期?

最佳答案

您需要传递与默认值 input_formats 之一匹配的日期的字符串表示 :

['%Y-%m-%d',      # '2006-10-25'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y'] # '10/25/06'

在您的情况下,例如:

'birthdate': '1956-01-01'

关于python - Django testCase 中的 POST 日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27239983/

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