diff --git a/django/newforms/fields.py b/django/newforms/fields.py index 26935140c3..11e6f51779 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -11,7 +11,7 @@ from django.utils.translation import ugettext from django.utils.encoding import StrAndUnicode, smart_unicode 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: from decimal import Decimal, DecimalException @@ -284,6 +284,8 @@ DEFAULT_DATETIME_INPUT_FORMATS = ( ) class DateTimeField(Field): + widget = DateTimeInput + def __init__(self, input_formats=None, *args, **kwargs): super(DateTimeField, self).__init__(*args, **kwargs) self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 62e2815d0b..6364a28897 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -8,6 +8,7 @@ except NameError: from sets import Set as set # Python 2.3 fallback import copy +import datetime from itertools import chain from django.utils.datastructures import MultiValueDict @@ -19,7 +20,7 @@ from util import flatatt __all__ = ( 'Widget', 'TextInput', 'PasswordInput', 'HiddenInput', 'MultipleHiddenInput', - 'FileInput', 'Textarea', 'CheckboxInput', + 'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput', 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', 'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget', ) @@ -133,7 +134,7 @@ class FileInput(Input): def render(self, name, value, attrs=None): return super(FileInput, self).render(name, None, attrs=attrs) - + def value_from_datadict(self, data, files, name): "File widgets take data from FILES, not POST" return files.get(name, None) @@ -151,6 +152,19 @@ class Textarea(Widget): final_attrs = self.build_attrs(attrs, name=name) return u'%s' % (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): def __init__(self, attrs=None, check_test=bool): super(CheckboxInput, self).__init__(attrs) @@ -432,5 +446,5 @@ class SplitDateTimeWidget(MultiWidget): def decompress(self, value): if value: - return [value.date(), value.time()] + return [value.date(), value.time().replace(microsecond=0)] return [None, None] diff --git a/docs/newforms.txt b/docs/newforms.txt index 24294d68f2..5e33a478ee 100644 --- a/docs/newforms.txt +++ b/docs/newforms.txt @@ -1201,7 +1201,7 @@ If no ``input_formats`` argument is provided, the default input formats are:: ``DateTimeField`` ~~~~~~~~~~~~~~~~~ - * Default widget: ``TextInput`` + * Default widget: ``DateTimeInput`` * Empty value: ``None`` * Normalizes to: A Python ``datetime.datetime`` object. * 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', # '10/25/06' +**New in Django development version:** The ``DateTimeField`` used to use a +``TextInput`` widget by default. This has now changed. + ``DecimalField`` ~~~~~~~~~~~~~~~~ @@ -1558,6 +1561,7 @@ commonly used groups of widgets: ``MultipleHiddenInput`` Multiple ``...`` ``CheckboxInput`` ``