#11231: forms.DateField, initial doesn't print out in input_formats format
------------------------------------+---------------------------------------
          Reporter:  skm            |         Owner:  nobody
            Status:  closed         |     Milestone:        
         Component:  Uncategorized  |       Version:  1.0   
        Resolution:  duplicate      |      Keywords:        
             Stage:  Unreviewed     |     Has_patch:  0     
        Needs_docs:  0              |   Needs_tests:  0     
Needs_better_patch:  0              |  
------------------------------------+---------------------------------------
Comment (by kmtracey):

 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(initial=date.today()-timedelta(days=1), 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(initial=date.today()-timedelta(days=1),
 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.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11231#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to