Python django测试用例中的POST日期值
我正在尝试编写一个测试来注册一个用户。我无法验证表单,因为我无法确定如何向表单提交有效日期。以下是违规代码:Python django测试用例中的POST日期值,python,django,testing,Python,Django,Testing,我正在尝试编写一个测试来注册一个用户。我无法验证表单,因为我无法确定如何向表单提交有效日期。以下是违规代码: class AccountManagementTest(TestCase): def test_signup(self): response = self.client.post(reverse('register'), { 'first_name': 'Gandalf', 'last_name': 'TheGre
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})
我可以在之后输出响应。内容
,表单中的错误是“请输入有效日期”。我没有覆盖表单中出生日期
的清除方法。以下是表单中日期字段的声明:
birthdate = forms.DateField(
widget=SelectDateWidget(
years=range(this_year-90, this_year-12)[::-1]),
required=True,)
我该如何发送一个有效的日期呢?在Python中,datetime和date不是同一个对象。
datetime包含小时、分钟、秒等信息
请看这里:
如果birthdate是Django中的日期字段,则需要编写:
'birthdate':日期(1956,1,1)
(不要忘记为此导入datetime.date)您需要传递与默认日期之一匹配的日期字符串表示形式:
在您的情况下,它可能是,例如:
'birthdate': '1956-01-01'
'birthdate': '1956-01-01'