Index: django/forms/fields.py
===================================================================
--- django/forms/fields.py (revision 8983)
+++ django/forms/fields.py (working copy)
@@ -28,7 +28,7 @@
from django.utils.encoding import smart_unicode, smart_str
from util import ErrorList, ValidationError
-from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput, TimeInput, SplitHiddenDateTimeWidget
+from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, SplitDateTimeWidget, SplitHiddenDateTimeWidget
from django.core.files.uploadedfile import SimpleUploadedFile as UploadedFile
__all__ = (
@@ -283,6 +283,7 @@
)
class DateField(Field):
+ widget = DateInput
default_error_messages = {
'invalid': _(u'Enter a valid date.'),
}
@@ -843,19 +844,20 @@
self.widget.choices = self.choices
class SplitDateTimeField(MultiValueField):
+ widget = SplitDateTimeWidget
hidden_widget = SplitHiddenDateTimeWidget
default_error_messages = {
'invalid_date': _(u'Enter a valid date.'),
'invalid_time': _(u'Enter a valid time.'),
}
- def __init__(self, *args, **kwargs):
+ def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs):
errors = self.default_error_messages.copy()
if 'error_messages' in kwargs:
errors.update(kwargs['error_messages'])
fields = (
- DateField(error_messages={'invalid': errors['invalid_date']}),
- TimeField(error_messages={'invalid': errors['invalid_time']}),
+ DateField(input_formats=input_date_formats, error_messages={'invalid': errors['invalid_date']}),
+ TimeField(input_formats=input_time_formats, error_messages={'invalid': errors['invalid_time']}),
)
super(SplitDateTimeField, self).__init__(fields, *args, **kwargs)
Index: django/forms/widgets.py
===================================================================
--- django/forms/widgets.py (revision 8983)
+++ django/forms/widgets.py (working copy)
@@ -23,7 +23,7 @@
__all__ = (
'Media', 'MediaDefiningClass', 'Widget', 'TextInput', 'PasswordInput',
'HiddenInput', 'MultipleHiddenInput',
- 'FileInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
+ 'FileInput', 'DateInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput',
'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
'CheckboxSelectMultiple', 'MultiWidget',
'SplitDateTimeWidget',
@@ -286,6 +286,23 @@
return mark_safe(u'' % (flatatt(final_attrs),
conditional_escape(force_unicode(value))))
+class DateInput(Input):
+ input_type = 'text'
+ format = '%Y-%m-%d' # '2006-10-25'
+
+ def __init__(self, attrs=None, format=None):
+ super(DateInput, self).__init__(attrs)
+ if format:
+ self.format = format
+
+ def render(self, name, value, attrs=None):
+ if value is None:
+ value = ''
+ elif hasattr(value, 'strftime'):
+ value = datetime_safe.new_date(value)
+ value = value.strftime(self.format)
+ return super(DateInput, self).render(name, value, attrs)
+
class DateTimeInput(Input):
input_type = 'text'
format = '%Y-%m-%d %H:%M:%S' # '2006-10-25 14:30:59'
@@ -305,12 +322,18 @@
class TimeInput(Input):
input_type = 'text'
+ format = '%H:%M:%S' # '14:30:59'
+ def __init__(self, attrs=None, format=None):
+ super(TimeInput, self).__init__(attrs)
+ if format:
+ self.format = format
+
def render(self, name, value, attrs=None):
if value is None:
value = ''
- elif isinstance(value, time):
- value = value.replace(microsecond=0)
+ elif hasattr(value, 'strftime'):
+ value = value.strftime(self.format)
return super(TimeInput, self).render(name, value, attrs)
class CheckboxInput(Widget):
@@ -655,8 +678,16 @@
"""
A Widget that splits datetime input into two boxes.
"""
- def __init__(self, attrs=None):
- widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs))
+ date_format = DateInput.format
+ time_format = TimeInput.format
+
+ def __init__(self, attrs=None, date_format=None, time_format=None):
+ if date_format:
+ self.date_format = date_format
+ if time_format:
+ self.time_format = time_format
+ widgets = (DateInput(attrs=attrs, format=self.date_format),
+ TimeInput(attrs=attrs, format=self.time_format))
super(SplitDateTimeWidget, self).__init__(widgets, attrs)
def decompress(self, value):
@@ -671,4 +702,3 @@
def __init__(self, attrs=None):
widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs))
super(SplitDateTimeWidget, self).__init__(widgets, attrs)
-
Index: django/contrib/localflavor/generic/forms.py
===================================================================
--- django/contrib/localflavor/generic/forms.py (revision 8983)
+++ django/contrib/localflavor/generic/forms.py (working copy)
@@ -36,3 +36,13 @@
def __init__(self, input_formats=None, *args, **kwargs):
input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
super(DateTimeField, self).__init__(input_formats=input_formats, *args, **kwargs)
+
+class SplitDateTimeField(forms.SplitDateTimeField):
+ """
+ Split date and time input fields which use non-US date and time input
+ formats by default.
+ """
+ def __init__(self, input_date_formats=None, input_time_formats=None, *args, **kwargs):
+ input_date_formats = input_date_formats or DEFAULT_DATE_INPUT_FORMATS
+ super(SplitDateTimeField, self).__init__(input_date_formats=input_date_formats,
+ input_time_formats=input_time_formats, *args, **kwargs)
Index: tests/regressiontests/forms/widgets.py
===================================================================
--- tests/regressiontests/forms/widgets.py (revision 8983)
+++ tests/regressiontests/forms/widgets.py (working copy)
@@ -1071,6 +1071,11 @@
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
u''
+Use 'date_format' and 'time_format' to change the way a value is displayed.
+>>> w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M')
+>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
+u''
+
>>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:40:00'])
False
>>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:41:00'])
@@ -1093,6 +1098,34 @@
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
u''
+Use 'format' to change the way a value is displayed.
+>>> w = DateTimeInput(format='%d/%m/%Y %H:%M')
+>>> w.render('date', d)
+u''
+
+# DateInput ###################################################################
+
+>>> w = DateInput()
+>>> w.render('date', None)
+u''
+>>> d = datetime.date(2007, 9, 17)
+>>> print d
+2007-09-17
+
+>>> w.render('date', d)
+u''
+>>> w.render('date', datetime.date(2007, 9, 17))
+u''
+
+We should be able to initialize from a unicode value.
+>>> w.render('date', u'2007-09-17')
+u''
+
+Use 'format' to change the way a value is displayed.
+>>> w = DateInput(format='%d/%m/%Y')
+>>> w.render('date', d)
+u''
+
# TimeInput ###################################################################
>>> w = TimeInput()
@@ -1114,6 +1147,11 @@
>>> w.render('time', u'13:12:11')
u''
+Use 'format' to change the way a value is displayed.
+>>> w = TimeInput(format='%H:%M')
+>>> w.render('time', t)
+u''
+
# SplitHiddenDateTimeWidget ###################################################
>>> from django.forms.widgets import SplitHiddenDateTimeWidget
@@ -1121,6 +1159,9 @@
>>> w = SplitHiddenDateTimeWidget()
>>> w.render('date', '')
u''
+>>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
+>>> print d
+2007-09-17 12:51:34.482548
>>> w.render('date', d)
u''
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
Index: AUTHORS
===================================================================
--- AUTHORS (revision 8983)
+++ AUTHORS (working copy)
@@ -239,6 +239,7 @@
Eugene Lazutkin
lcordier@point45.com
Jeong-Min Lee
+ Tai Lee
Jannis Leidel
Christopher Lenz
lerouxb@gmail.com
@@ -278,7 +279,6 @@
Aljosa Mohorovic
Ramiro Morales
Eric Moritz
- mrmachine
Robin Munn
James Murty
msundstr
Index: docs/ref/contrib/localflavor.txt
===================================================================
--- docs/ref/contrib/localflavor.txt (revision 8983)
+++ docs/ref/contrib/localflavor.txt (working copy)
@@ -65,9 +65,9 @@
* `United States of America`_
The ``django.contrib.localflavor`` package also includes a ``generic`` subpackage,
-containing useful code that is not specific to one particular country or
-culture. Currently, it defines date and datetime input fields based on those
-from :ref:`forms `, but with non-US default formats.
+containing useful code that is not specific to one particular country or culture.
+Currently, it defines date, datetime and split datetime input fields based on
+those from :ref:`forms `, but with non-US default formats.
Here's an example of how to use them::
from django import forms
Index: docs/ref/forms/fields.txt
===================================================================
--- docs/ref/forms/fields.txt (revision 8983)
+++ docs/ref/forms/fields.txt (working copy)
@@ -396,7 +396,7 @@
.. class:: DateField(**kwargs)
- * Default widget: ``TextInput``
+ * Default widget: ``DateInput``
* Empty value: ``None``
* Normalizes to: A Python ``datetime.date`` object.
* Validates that the given value is either a ``datetime.date``,
@@ -418,6 +418,10 @@
'%B %d %Y', '%B %d, %Y', # 'October 25 2006', 'October 25, 2006'
'%d %B %Y', '%d %B, %Y', # '25 October 2006', '25 October, 2006'
+.. versionchanged:: development version
+ The ``DateField`` previously used a ``TextInput`` widget by default. It now
+ uses a ``DateInput`` widget.
+
``DateTimeField``
~~~~~~~~~~~~~~~~~
@@ -737,6 +741,36 @@
.. class:: SplitDateTimeField(**kwargs)
+ * Default widget: ``SplitDateTimeWidget``
+ * Empty value: ``None``
+ * Normalizes to: A Python ``datetime.datetime`` object.
+ * Validates that the given value is a ``datetime.datetime`` or string
+ formatted in a particular datetime format.
+ * Error message keys: ``required``, ``invalid``
+
+Takes two optional arguments:
+
+.. attribute:: SplitDateTimeField.input_date_formats
+
+ A list of formats used to attempt to convert a string to a valid
+ ``datetime.date`` object.
+
+If no ``input_date_formats`` argument is provided, the default input formats
+for ``DateField`` are used.
+
+.. attribute:: SplitDateTimeField.input_time_formats
+
+ A list of formats used to attempt to convert a string to a valid
+ ``datetime.time`` object.
+
+If no ``input_time_formats`` argument is provided, the default input formats
+for ``TimeField`` are used.
+
+.. versionchanged:: development version
+ The ``SplitDateTimeField`` previously used two ``TextInput`` widgets by
+ default. The ``input_date_formats`` and ``input_time_formats`` arguments
+ are also new in the development version.
+
Fields which handle relationships
---------------------------------
Index: docs/ref/forms/widgets.txt
===================================================================
--- docs/ref/forms/widgets.txt (revision 8983)
+++ docs/ref/forms/widgets.txt (working copy)
@@ -36,12 +36,49 @@
File upload input: ````
+.. class:: DateInput
+
+ .. versionadded:: development version
+
+ Date input as a simple text box: ````
+
+ Takes one optional argument:
+
+ .. attribute:: DateInput.format
+
+ The format in which this field's initial value will be displayed.
+
+ If no ``format`` argument is provided, the default format is ``'%Y-%m-%d'``.
+
.. class:: DateTimeInput
.. versionadded:: 1.0
Date/time input as a simple text box: ````
+ Takes one optional argument:
+
+ .. attribute:: DateTimeInput.format
+
+ The format in which this field's initial value will be displayed.
+
+ If no ``format`` argument is provided, the default format is ``'%Y-%m-%d %H:%M:%S'``.
+
+.. class:: TimeInput
+
+ Time input as a simple text box: ````
+
+ Takes one optional argument:
+
+ .. attribute:: TimeInput.format
+
+ The format in which this field's initial value will be displayed.
+
+ If no ``format`` argument is provided, the default format is ``'%H:%M:%S'``.
+
+ .. versionchanged:: development version
+ The ``format`` argument is new in the development version.
+
.. class:: Textarea
Text area: ````
@@ -91,9 +128,12 @@
.. class:: SplitDateTimeWidget
- Wrapper around two ``TextInput`` widgets: one for the date, and one for the
- time.
+ Wrapper around two widgets: ``DateInput`` for the date, and ``TimeInput``
+ for the time.
+ Takes two optional arguments, ``date_format`` and ``time_format``, which
+ work just like the ``format`` argument for ``DateInput`` and ``TimeInput``.
+
Specifying widgets
------------------