Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST date values in django testCase

I am trying to write a test to sign up a user. I can't get the form to validate because I can't figure out how to submit a valid date to the form. Here is the offending code:

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

I can output the response.content afterwards and the error in the form is 'Please enter a valid date.' I have not overridden the clean method for birthdate in the form. Here's the declaration of the date field from the form:

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

How the heck do I send it a valid date?

like image 232
raphael Avatar asked Dec 02 '14 00:12

raphael


1 Answers

You need to pass a string representation of the date which matches one of the default input_formats:

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

In your case it could be, for example:

'birthdate': '1956-01-01'
like image 55
alecxe Avatar answered Oct 15 '22 20:10

alecxe