#3672: newforms: DateField doesn't handle date output formats
----------------------------------------+-----------------------------------
          Reporter:  [EMAIL PROTECTED]  |         Owner:  ajs                   
                         
            Status:  new                |     Milestone:                        
                         
         Component:  django.newforms    |       Version:  SVN                   
                         
        Resolution:                     |      Keywords:  DateField l10n 
localization format input output
             Stage:  Accepted           |     Has_patch:  1                     
                         
        Needs_docs:  0                  |   Needs_tests:  1                     
                         
Needs_better_patch:  1                  |  
----------------------------------------+-----------------------------------
Comment (by Dinoboff):

 Thanks for the patches. I am new to Python and Django can't really
 understand how all that works.

 Here the solution for !DateField that I am using; it doesn't rely on
 settings.DATE_FORMAT but on the field input_formats property:

 {{{
 from django import newforms as forms
 from django.newforms.fields import DEFAULT_DATE_INPUT_FORMATS
 import datetime

 DEFAULT_DATE_OUTPUT_FORMATS = DEFAULT_DATE_INPUT_FORMATS[0]

 class FormattedTextInput(forms.widgets.TextInput):
     "Overrides TextInput to render formatted value."
     def render(self, name, value, attrs=None):
         formatted_value = None
         if value:
             formatted_value = self.format_value(value)
         return super(FormattedTextInput, self).render(name,
 formatted_value, attrs)

 class DateFormattedTextInput(FormattedTextInput):
     "Renders formatted date."
     def __init__(self, format=None, attrs=None):
         super(DateFormattedTextInput, self).__init__(attrs)
         self.format = format or DEFAULT_DATE_OUTPUT_FORMATS

     def format_value(self, value):
         if isinstance(value, datetime.date) or isinstance(value,
 datetime.datetime):
             return value.strftime(self.format)
         else:
             return value

 class DateField(forms.DateField):
     widget = DateFormattedTextInput

     def __init__(self, *args, **kwargs):
         super(DateField, self).__init__(*args, **kwargs)
         self.widget.format = self.input_formats[0]
 }}}

 Hope it helps.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/3672#comment:14>
Django Code <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