Ticket #7509: splitdatetimewidget_formats.diff

File splitdatetimewidget_formats.diff, 2.0 KB (added by Jonathan Buchanan, 16 years ago)

Adds date_format and time_format arguments

  • django/newforms/widgets.py

     
    371371                label_for = u' for="%s"' % final_attrs['id']
    372372            else:
    373373                label_for = ''
    374                
     374
    375375            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
    376376            option_value = force_unicode(option_value)
    377377            rendered_cb = cb.render(name, option_value)
     
    468468    """
    469469    A Widget that splits datetime input into two <input type="text"> boxes.
    470470    """
    471     def __init__(self, attrs=None):
    472         widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs))
     471    def __init__(self, attrs=None, date_format='%Y-%m-%d', time_format='%H:%M:%S'):
     472        widgets = (DateTimeInput(attrs=attrs, format=date_format),
     473                   DateTimeInput(attrs=attrs, format=time_format))
    473474        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
    474475
    475476    def decompress(self, value):
  • tests/regressiontests/forms/widgets.py

     
    907907>>> w.render('date', [datetime.date(2006, 1, 10), datetime.time(7, 30)])
    908908u'<input type="text" name="date_0" value="2006-01-10" /><input type="text" name="date_1" value="07:30:00" />'
    909909
     910You can also pass custom date and time formats to the constructor.
     911>>> w = SplitDateTimeWidget(date_format='%m/%d/%Y', time_format='%I:%M:%S %p')
     912>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
     913u'<input type="text" name="date_0" value="01/10/2006" /><input type="text" name="date_1" value="07:30:00 AM" />'
     914
    910915You can also pass 'attrs' to the constructor. In this case, the attrs will be
    911916included on both widgets.
    912917>>> w = SplitDateTimeWidget(attrs={'class': 'pretty'})
Back to Top