From 74ffca17e2ca6a9f061913797efeebbc578692c5 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Tue, 8 Feb 2011 12:00:21 +0000 Subject: [PATCH] Fixed #10573 -- Corrected autofocus problem in admin when the first widget displayed is a multiwidget. Thanks to rduffield for the report, and to Ramiro and Julien Phalip for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15452 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../admin/templates/admin/change_form.html | 6 ++--- django/forms/forms.py | 11 +++++++++ tests/regressiontests/admin_views/models.py | 5 ++++ tests/regressiontests/admin_views/tests.py | 23 +++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html index 8f8dfe6143..fb278b42be 100644 --- a/django/contrib/admin/templates/admin/change_form.html +++ b/django/contrib/admin/templates/admin/change_form.html @@ -17,8 +17,8 @@ {% block breadcrumbs %}{% if not is_popup %} {% endif %}{% endblock %} @@ -60,7 +60,7 @@ {% submit_row %} {% if adminform and add %} - + {% endif %} {# JavaScript for prepopulated fields #} diff --git a/django/forms/forms.py b/django/forms/forms.py index 1cf6f2a3a2..f776861948 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -522,3 +522,14 @@ class BoundField(StrAndUnicode): return self.html_name return '' auto_id = property(_auto_id) + + def _id_for_label(self): + """ + Wrapper around the field widget's `id_for_label` class method. + Useful, for example, for focusing on this field regardless of whether + it has a single widget or a MutiWidget. + """ + widget = self.field.widget + id_ = widget.attrs.get('id') or self.auto_id + return widget.id_for_label(id_) + id_for_label = property(_id_for_label) diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index 02c6b9208d..60319eaf7d 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -666,6 +666,10 @@ class Answer(models.Model): def __unicode__(self): return self.answer +class Reservation(models.Model): + start_date = models.DateTimeField() + price = models.IntegerField() + admin.site.register(Article, ArticleAdmin) admin.site.register(CustomArticle, CustomArticleAdmin) admin.site.register(Section, save_as=True, inlines=[ArticleInline]) @@ -701,6 +705,7 @@ admin.site.register(PlotDetails) admin.site.register(CyclicOne) admin.site.register(CyclicTwo) admin.site.register(WorkHour, WorkHourAdmin) +admin.site.register(Reservation) # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2. # That way we cover all four cases: diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index a05f40f702..98a2d41ddc 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -431,6 +431,29 @@ class AdminViewBasicTest(TestCase): except SuspiciousOperation: self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model") +class AdminJavaScriptTest(AdminViewBasicTest): + def testSingleWidgetFirsFieldFocus(self): + """ + JavaScript-assisted auto-focus on first field. + """ + response = self.client.get('/test_admin/%s/admin_views/picture/add/' % self.urlbit) + self.assertContains( + response, + '' + ) + + def testMultiWidgetFirsFieldFocus(self): + """ + JavaScript-assisted auto-focus should work if a model/ModelAdmin setup + is such that the first form field has a MultiWidget. + """ + response = self.client.get('/test_admin/%s/admin_views/reservation/add/' % self.urlbit) + self.assertContains( + response, + '' + ) + + class SaveAsTests(TestCase): fixtures = ['admin-views-users.xml','admin-views-person.xml']