Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#11231 closed (duplicate)

forms.DateField, initial doesn't print out in input_formats format

Reported by: skm Owned by: nobody
Component: Uncategorized Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


start_date = forms.DateField(, input_formats=[DATE_FORMAT], widget=forms.TextInput(attrs={"readonly":"true"}))

I can use strftime() but then in view code, where I am adding timedelta, accessing SearchForm.base_fieldsstart_date?.initial doesnt return a datetime object, it returns a string

As well as that, if the value isn't there the format ends up being different to when someone selects a date.

Change History (2)

comment:1 Changed 9 years ago by James Bennett

Resolution: duplicate
Status: newclosed

This feels like part of #7980, which is being tackled by a Summer of Code project.

comment:2 Changed 9 years ago by Karen Tracey

Not sure this is a dupe of #7980, rather I think it is invalid. Some actual code would help understand what you are trying to say here. Based on the summary, and the fact that you are specifying input_formats for a field you are making read-only, though, it sounds like you are trying to use input_formats to control how the date is formatted on output. It's not used for that. It controls what formats are acceptable on input. With what you specify, the initial date value as displayed on the form will not necessarily match what you are apparently looking for:

>>> from django import forms
>>> from datetime import date, timedelta
>>> class DF(forms.Form):
...     start_date = forms.DateField(, input_formats=["%Y %m %d"], widget=forms.TextInput(attrs={"readonly":"true"}))
>>> df = DF()
>>> df.as_p()
u'<p><label for="id_start_date">Start date:</label> <input readonly="true" type="text" name="start_date" value="2009-05-28" id="id_start_date" /></p>'

The value on display doesn't match the no-dashes format specified for input_formats because input_formats does not control output display. To control the way the initial date is formatted for output, you can use the format parameter of the DateInput widget:

>>> class DF2(forms.Form):
...     start_date = forms.DateField(, widget=forms.DateInput(format="%Y %m %d", attrs={"readonly":"true"}))
>>> df2 = DF2()
>>> df2.as_p()
u'<p><label for="id_start_date">Start date:</label> <input readonly="true" type="text" name="start_date" value="2009 05 28" id="id_start_date" /></p>'

Note the DateInput widget was added in r10115 for 1.1 so you'll need to be using the 1.1 beta release or svn trunk version to use it.

Note: See TracTickets for help on using tickets.
Back to Top