我正在尝试编写一个测试来注册用户。我无法验证表单,因为我不知道如何向表单提交有效日期。这是有问题的代码:
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
并且表单中的错误是“请输入有效日期”。我没有覆盖表单中 birthdate
的 clean
方法。这是表单中日期字段的声明:
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/