Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #12073 -- Made `AdminDateWidget` and `AdminTimeWidget` consider…

… user-supplied `attrs`. Thanks to elliss for the report and to koenb for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17125 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5c9901323ec07c399efbcf0fc6886a5f8a3f8589 1 parent 119e188
@jphalip jphalip authored
View
14 django/contrib/admin/widgets.py
@@ -53,8 +53,11 @@ def media(self):
js = ["calendar.js", "admin/DateTimeShortcuts.js"]
return forms.Media(js=[static("admin/js/%s" % path) for path in js])
- def __init__(self, attrs={}, format=None):
- super(AdminDateWidget, self).__init__(attrs={'class': 'vDateField', 'size': '10'}, format=format)
+ def __init__(self, attrs=None, format=None):
+ final_attrs = {'class': 'vDateField', 'size': '10'}
+ if attrs is not None:
+ final_attrs.update(attrs)
+ super(AdminDateWidget, self).__init__(attrs=final_attrs, format=format)
class AdminTimeWidget(forms.TimeInput):
@@ -63,8 +66,11 @@ def media(self):
js = ["calendar.js", "admin/DateTimeShortcuts.js"]
return forms.Media(js=[static("admin/js/%s" % path) for path in js])
- def __init__(self, attrs={}, format=None):
- super(AdminTimeWidget, self).__init__(attrs={'class': 'vTimeField', 'size': '8'}, format=format)
+ def __init__(self, attrs=None, format=None):
+ final_attrs = {'class': 'vTimeField', 'size': '8'}
+ if attrs is not None:
+ final_attrs.update(attrs)
+ super(AdminTimeWidget, self).__init__(attrs=final_attrs, format=format)
class AdminSplitDateTime(forms.SplitDateTimeWidget):
"""
View
35 tests/regressiontests/admin_widgets/tests.py
@@ -205,6 +205,41 @@ def test_stacked_render(self):
'<select multiple="multiple" name="test" class="selectfilterstacked">\n</select><script type="text/javascript">addEvent(window, "load", function(e) {SelectFilter.init("id_test", "test", 1, "%(ADMIN_MEDIA_PREFIX)s"); });</script>\n' % admin_media_prefix()
)
+class AdminDateWidgetTest(DjangoTestCase):
+ def test_attrs(self):
+ """
+ Ensure that user-supplied attrs are used.
+ Refs #12073.
+ """
+ w = widgets.AdminDateWidget()
+ self.assertEqual(
+ conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
+ '<input value="2007-12-01" type="text" class="vDateField" name="test" size="10" />',
+ )
+ # pass attrs to widget
+ w = widgets.AdminDateWidget(attrs={'size': 20, 'class': 'myDateField'})
+ self.assertEqual(
+ conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
+ '<input value="2007-12-01" type="text" class="myDateField" name="test" size="20" />',
+ )
+
+class AdminTimeWidgetTest(DjangoTestCase):
+ def test_attrs(self):
+ """
+ Ensure that user-supplied attrs are used.
+ Refs #12073.
+ """
+ w = widgets.AdminTimeWidget()
+ self.assertEqual(
+ conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
+ '<input value="09:30:00" type="text" class="vTimeField" name="test" size="8" />',
+ )
+ # pass attrs to widget
+ w = widgets.AdminTimeWidget(attrs={'size': 20, 'class': 'myTimeField'})
+ self.assertEqual(
+ conditional_escape(w.render('test', datetime(2007, 12, 1, 9, 30))),
+ '<input value="09:30:00" type="text" class="myTimeField" name="test" size="20" />',
+ )
class AdminSplitDateTimeWidgetTest(DjangoTestCase):
def test_render(self):
Please sign in to comment.
Something went wrong with that request. Please try again.