Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed the default form presentation of datetime values to not inclu…

…de 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
  • Loading branch information...
commit 1671fc48ab9f1c73ef04a10f0eb9e1f8452cd004 1 parent f5831b0
@malcolmt malcolmt authored
View
4 django/newforms/fields.py
@@ -11,7 +11,7 @@
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 @@ def clean(self, value):
)
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
View
20 django/newforms/widgets.py
@@ -8,6 +8,7 @@
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 @@
__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 @@ def render(self, name, value, attrs=None):
final_attrs = self.build_attrs(attrs, name=name)
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):
def __init__(self, attrs=None, check_test=bool):
super(CheckboxInput, self).__init__(attrs)
@@ -432,5 +446,5 @@ def __init__(self, attrs=None):
def decompress(self, value):
if value:
- return [value.date(), value.time()]
+ return [value.date(), value.time().replace(microsecond=0)]
return [None, None]
View
9 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 ``<input type='hidden' ...``
instances.
``FileInput`` ``<input type='file' ...``
+ ``DateTimeInput`` ``<input type='text' ...``
``Textarea`` ``<textarea>...</textarea>``
``CheckboxInput`` ``<input type='checkbox' ...``
``Select`` ``<select><option ...``
@@ -1571,6 +1575,9 @@ commonly used groups of widgets:
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
------------------
View
15 tests/regressiontests/forms/widgets.py
@@ -851,4 +851,19 @@
>>> w = SplitDateTimeWidget(attrs={'class': 'pretty'})
>>> 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" />'
+
+# 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" />'
"""
Please sign in to comment.
Something went wrong with that request. Please try again.