Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Malcolm Tredinnick authored October 21, 2007
4  django/newforms/fields.py
@@ -11,7 +11,7 @@
11 11
 from django.utils.encoding import StrAndUnicode, smart_unicode
12 12
 
13 13
 from util import ErrorList, ValidationError
14  
-from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple
  14
+from widgets import TextInput, PasswordInput, HiddenInput, MultipleHiddenInput, FileInput, CheckboxInput, Select, NullBooleanSelect, SelectMultiple, DateTimeInput
15 15
 
16 16
 try:
17 17
     from decimal import Decimal, DecimalException
@@ -284,6 +284,8 @@ def clean(self, value):
284 284
 )
285 285
 
286 286
 class DateTimeField(Field):
  287
+    widget = DateTimeInput
  288
+
287 289
     def __init__(self, input_formats=None, *args, **kwargs):
288 290
         super(DateTimeField, self).__init__(*args, **kwargs)
289 291
         self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
20  django/newforms/widgets.py
@@ -8,6 +8,7 @@
8 8
     from sets import Set as set   # Python 2.3 fallback
9 9
 
10 10
 import copy
  11
+import datetime
11 12
 from itertools import chain
12 13
 
13 14
 from django.utils.datastructures import MultiValueDict
@@ -19,7 +20,7 @@
19 20
 __all__ = (
20 21
     'Widget', 'TextInput', 'PasswordInput',
21 22
     'HiddenInput', 'MultipleHiddenInput',
22  
-    'FileInput', 'Textarea', 'CheckboxInput',
  23
+    'FileInput', 'DateTimeInput', 'Textarea', 'CheckboxInput',
23 24
     'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect',
24 25
     'CheckboxSelectMultiple', 'MultiWidget', 'SplitDateTimeWidget',
25 26
 )
@@ -133,7 +134,7 @@ class FileInput(Input):
133 134
 
134 135
     def render(self, name, value, attrs=None):
135 136
         return super(FileInput, self).render(name, None, attrs=attrs)
136  
-        
  137
+
137 138
     def value_from_datadict(self, data, files, name):
138 139
         "File widgets take data from FILES, not POST"
139 140
         return files.get(name, None)
@@ -151,6 +152,19 @@ def render(self, name, value, attrs=None):
151 152
         final_attrs = self.build_attrs(attrs, name=name)
152 153
         return u'<textarea%s>%s</textarea>' % (flatatt(final_attrs), escape(value))
153 154
 
  155
+class DateTimeInput(Input):
  156
+    input_type = 'text'
  157
+    format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'
  158
+
  159
+    def __init__(self, attrs=None, format=None):
  160
+        super(DateTimeInput, self).__init__(attrs)
  161
+        if format:
  162
+            self.format = format
  163
+
  164
+    def render(self, name, value, attrs=None):
  165
+        return super(DateTimeInput, self).render(name,
  166
+                value.strftime(self.format), attrs)
  167
+
154 168
 class CheckboxInput(Widget):
155 169
     def __init__(self, attrs=None, check_test=bool):
156 170
         super(CheckboxInput, self).__init__(attrs)
@@ -432,5 +446,5 @@ def __init__(self, attrs=None):
432 446
 
433 447
     def decompress(self, value):
434 448
         if value:
435  
-            return [value.date(), value.time()]
  449
+            return [value.date(), value.time().replace(microsecond=0)]
436 450
         return [None, None]
9  docs/newforms.txt
@@ -1201,7 +1201,7 @@ If no ``input_formats`` argument is provided, the default input formats are::
1201 1201
 ``DateTimeField``
1202 1202
 ~~~~~~~~~~~~~~~~~
1203 1203
 
1204  
-    * Default widget: ``TextInput``
  1204
+    * Default widget: ``DateTimeInput``
1205 1205
     * Empty value: ``None``
1206 1206
     * Normalizes to: A Python ``datetime.datetime`` object.
1207 1207
     * 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::
1222 1222
     '%m/%d/%y %H:%M',        # '10/25/06 14:30'
1223 1223
     '%m/%d/%y',              # '10/25/06'
1224 1224
 
  1225
+**New in Django development version:** The ``DateTimeField`` used to use a
  1226
+``TextInput`` widget by default. This has now changed.
  1227
+
1225 1228
 ``DecimalField``
1226 1229
 ~~~~~~~~~~~~~~~~
1227 1230
 
@@ -1558,6 +1561,7 @@ commonly used groups of widgets:
1558 1561
     ``MultipleHiddenInput``       Multiple ``<input type='hidden' ...``
1559 1562
                                   instances.
1560 1563
     ``FileInput``                 ``<input type='file' ...``
  1564
+    ``DateTimeInput``             ``<input type='text' ...``
1561 1565
     ``Textarea``                  ``<textarea>...</textarea>``
1562 1566
     ``CheckboxInput``             ``<input type='checkbox' ...``
1563 1567
     ``Select``                    ``<select><option ...``
@@ -1571,6 +1575,9 @@ commonly used groups of widgets:
1571 1575
                                   one for the Date, and one for the Time.
1572 1576
     ============================  ===========================================
1573 1577
 
  1578
+**New in Django development version:** The ``DateTimeInput`` has been added
  1579
+since the last release.
  1580
+
1574 1581
 Specifying widgets
1575 1582
 ------------------
1576 1583
 
15  tests/regressiontests/forms/widgets.py
@@ -851,4 +851,19 @@
851 851
 >>> w = SplitDateTimeWidget(attrs={'class': 'pretty'})
852 852
 >>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
853 853
 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" />'
  854
+
  855
+# DateTimeInput ###############################################################
  856
+
  857
+>>> w = DateTimeInput()
  858
+>>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)
  859
+>>> print d
  860
+2007-09-17 12:51:34.482548
  861
+
  862
+The microseconds are trimmed on display, by default.
  863
+>>> w.render('date', d)
  864
+u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
  865
+>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51, 34))
  866
+u'<input type="text" name="date" value="2007-09-17 12:51:34" />'
  867
+>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
  868
+u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
854 869
 """

0 notes on commit 1671fc4

Please sign in to comment.
Something went wrong with that request. Please try again.