Changed the default form presentation of datetime values to not include the
fractional second values (they usually aren't going to be needed). Based on patches from yi.codeplayer@gmail.com, andrews and Wiliam Alves de Souza. Fixed #4428, #4487 git-svn-id: http://code.djangoproject.com/svn/django/trunk@6578 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
f5831b03c9
commit
1671fc48ab
|
@ -11,7 +11,7 @@ from django.utils.translation import ugettext
|
||||||
from django.utils.encoding import StrAndUnicode, smart_unicode
|
from django.utils.encoding import StrAndUnicode, smart_unicode
|
||||||
|
|
||||||
from util import ErrorList, ValidationError
|
from util import ErrorList, ValidationError
|
||||||
from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
|
from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from decimal import Decimal, DecimalException
|
from decimal import Decimal, DecimalException
|
||||||
|
@ -284,6 +284,8 @@ DEFAULT_DATETIME_INPUT_FORMATS = (
|
||||||
)
|
)
|
||||||
|
|
||||||
class DateTimeField(Field):
|
class DateTimeField(Field):
|
||||||
|
widget = DateTimeInput
|
||||||
|
|
||||||
def __init__(self, input_formats=None, *args, **kwargs):
|
def __init__(self, input_formats=None, *args, **kwargs):
|
||||||
super(DateTimeField, self).__init__(*args, **kwargs)
|
super(DateTimeField, self).__init__(*args, **kwargs)
|
||||||
self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
|
self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
|
||||||
|
|
|
@ -8,6 +8,7 @@ except NameError:
|
||||||
from sets import Set as set # Python 2.3 fallback
|
from sets import Set as set # Python 2.3 fallback
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
import datetime
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
|
||||||
from django.utils.datastructures import MultiValueDict
|
from django.utils.datastructures import MultiValueDict
|
||||||
|
@ -19,7 +20,7 @@ from util import flatatt
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Widget', 'TextInput', 'PasswordInput',
|
'Widget', 'TextInput', 'PasswordInput',
|
||||||
'HiddenInput', 'MultipleHiddenInput',
|
'HiddenInput', 'MultipleHiddenInput',
|
||||||
'FileInput', 'Textarea', 'CheckboxInput',
|
'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
|
||||||
'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
|
'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
|
||||||
'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
|
'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
|
||||||
)
|
)
|
||||||
|
@ -151,6 +152,19 @@ class Textarea(Widget):
|
||||||
final_attrs = self.build_attrs(attrs, name=name)
|
final_attrs = self.build_attrs(attrs, name=name)
|
||||||
return u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))
|
return u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))
|
||||||
|
|
||||||
|
class DateTimeInput(Input):
|
||||||
|
input_type = 'text'
|
||||||
|
format = '%Y-%m-%d %H:%M:%S' # '2006-10-25 14:30:59'
|
||||||
|
|
||||||
|
def __init__(self, attrs=None, format=None):
|
||||||
|
super(DateTimeInput, self).__init__(attrs)
|
||||||
|
if format:
|
||||||
|
self.format = format
|
||||||
|
|
||||||
|
def render(self, name, value, attrs=None):
|
||||||
|
return super(DateTimeInput, self).render(name,
|
||||||
|
value.strftime(self.format), attrs)
|
||||||
|
|
||||||
class CheckboxInput(Widget):
|
class CheckboxInput(Widget):
|
||||||
def __init__(self, attrs=None, check_test=bool):
|
def __init__(self, attrs=None, check_test=bool):
|
||||||
super(CheckboxInput, self).__init__(attrs)
|
super(CheckboxInput, self).__init__(attrs)
|
||||||
|
@ -432,5 +446,5 @@ class SplitDateTimeWidget(MultiWidget):
|
||||||
|
|
||||||
def decompress(self, value):
|
def decompress(self, value):
|
||||||
if value:
|
if value:
|
||||||
return [value.date(), value.time()]
|
return [value.date(), value.time().replace(microsecond=0)]
|
||||||
return [None, None]
|
return [None, None]
|
||||||
|
|
|
@ -1201,7 +1201,7 @@ If no ``input_formats`` argument is provided, the default input formats are::
|
||||||
``DateTimeField``
|
``DateTimeField``
|
||||||
~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
* Default widget: ``TextInput``
|
* Default widget: ``DateTimeInput``
|
||||||
* Empty value: ``None``
|
* Empty value: ``None``
|
||||||
* Normalizes to: A Python ``datetime.datetime`` object.
|
* Normalizes to: A Python ``datetime.datetime`` object.
|
||||||
* Validates that the given value is either a ``datetime.datetime``,
|
* Validates that the given value is either a ``datetime.datetime``,
|
||||||
|
@ -1222,6 +1222,9 @@ If no ``input_formats`` argument is provided, the default input formats are::
|
||||||
'%m/%d/%y %H:%M', # '10/25/06 14:30'
|
'%m/%d/%y %H:%M', # '10/25/06 14:30'
|
||||||
'%m/%d/%y', # '10/25/06'
|
'%m/%d/%y', # '10/25/06'
|
||||||
|
|
||||||
|
**New in Django development version:** The ``DateTimeField`` used to use a
|
||||||
|
``TextInput`` widget by default. This has now changed.
|
||||||
|
|
||||||
``DecimalField``
|
``DecimalField``
|
||||||
~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
@ -1558,6 +1561,7 @@ commonly used groups of widgets:
|
||||||
``MultipleHiddenInput`` Multiple ``<input type='hidden' ...``
|
``MultipleHiddenInput`` Multiple ``<input type='hidden' ...``
|
||||||
instances.
|
instances.
|
||||||
``FileInput`` ``<input type='file' ...``
|
``FileInput`` ``<input type='file' ...``
|
||||||
|
``DateTimeInput`` ``<input type='text' ...``
|
||||||
``Textarea`` ``<textarea>...</textarea>``
|
``Textarea`` ``<textarea>...</textarea>``
|
||||||
``CheckboxInput`` ``<input type='checkbox' ...``
|
``CheckboxInput`` ``<input type='checkbox' ...``
|
||||||
``Select`` ``<select><option ...``
|
``Select`` ``<select><option ...``
|
||||||
|
@ -1571,6 +1575,9 @@ commonly used groups of widgets:
|
||||||
one for the Date, and one for the Time.
|
one for the Date, and one for the Time.
|
||||||
============================ ===========================================
|
============================ ===========================================
|
||||||
|
|
||||||
|
**New in Django development version:** The ``DateTimeInput`` has been added
|
||||||
|
since the last release.
|
||||||
|
|
||||||
Specifying widgets
|
Specifying widgets
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
|
|
@ -851,4 +851,19 @@ included on both widgets.
|
||||||
>>> w = SplitDateTimeWidget(attrs={'class': 'pretty'})
|
>>> w = SplitDateTimeWidget(attrs={'class': 'pretty'})
|
||||||
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
|
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
|
||||||
u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
|
u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
|
||||||
|
|
||||||
|
# DateTimeInput ###############################################################
|
||||||
|
|
||||||
|
>>> w = DateTimeInput()
|
||||||
|
>>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
|
||||||
|
>>> print d
|
||||||
|
2007-09-17 12:51:34.482548
|
||||||
|
|
||||||
|
The microseconds are trimmed on display, by default.
|
||||||
|
>>> w.render('date', d)
|
||||||
|
u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
|
||||||
|
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
|
||||||
|
u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
|
||||||
|
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
|
||||||
|
u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue