Code

Ticket #10573: 10573.2.diff

File 10573.2.diff, 4.4 KB (added by julien, 3 years ago)
Line 
1diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html
2index 8f8dfe6..fb278b4 100644
3--- a/django/contrib/admin/templates/admin/change_form.html
4+++ b/django/contrib/admin/templates/admin/change_form.html
5@@ -17,8 +17,8 @@
6 {% block breadcrumbs %}{% if not is_popup %}
7 <div class="breadcrumbs">
8      <a href="../../../">{% trans "Home" %}</a> &rsaquo;
9-     <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
10-     {% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} &rsaquo;
11+     <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
12+     {% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} &rsaquo;
13      {% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}
14 </div>
15 {% endif %}{% endblock %}
16@@ -60,7 +60,7 @@
17 {% submit_row %}
18 
19 {% if adminform and add %}
20-   <script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
21+   <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
22 {% endif %}
23 
24 {# JavaScript for prepopulated fields #}
25diff --git a/django/forms/forms.py b/django/forms/forms.py
26index 1cf6f2a..f776861 100644
27--- a/django/forms/forms.py
28+++ b/django/forms/forms.py
29@@ -522,3 +522,14 @@ class BoundField(StrAndUnicode):
30             return self.html_name
31         return ''
32     auto_id = property(_auto_id)
33+
34+    def _id_for_label(self):
35+        """
36+        Wrapper around the field widget's `id_for_label` class method.
37+        Useful, for example, for focusing on this field regardless of whether
38+        it has a single widget or a MutiWidget.
39+        """
40+        widget = self.field.widget
41+        id_ = widget.attrs.get('id') or self.auto_id
42+        return widget.id_for_label(id_)
43+    id_for_label = property(_id_for_label)
44diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
45index 02c6b92..60319ea 100644
46--- a/tests/regressiontests/admin_views/models.py
47+++ b/tests/regressiontests/admin_views/models.py
48@@ -666,6 +666,10 @@ class Answer(models.Model):
49     def __unicode__(self):
50         return self.answer
51 
52+class Reservation(models.Model):
53+    start_date = models.DateTimeField()
54+    price = models.IntegerField()
55+   
56 admin.site.register(Article, ArticleAdmin)
57 admin.site.register(CustomArticle, CustomArticleAdmin)
58 admin.site.register(Section, save_as=True, inlines=[ArticleInline])
59@@ -701,6 +705,7 @@ admin.site.register(PlotDetails)
60 admin.site.register(CyclicOne)
61 admin.site.register(CyclicTwo)
62 admin.site.register(WorkHour, WorkHourAdmin)
63+admin.site.register(Reservation)
64 
65 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
66 # That way we cover all four cases:
67diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
68index f392393..d4d58a2 100644
69--- a/tests/regressiontests/admin_views/tests.py
70+++ b/tests/regressiontests/admin_views/tests.py
71@@ -431,6 +431,29 @@ class AdminViewBasicTest(TestCase):
72         except SuspiciousOperation:
73             self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model")
74 
75+class AdminJavaScriptTest(AdminViewBasicTest):
76+    def testSingleWidgetFirsFieldFocus(self):
77+        """
78+        JavaScript-assisted auto-focus on first field.
79+        """
80+        response = self.client.get('/test_admin/%s/admin_views/picture/add/' % self.urlbit)
81+        self.assertContains(
82+            response,
83+            '<script type="text/javascript">document.getElementById("id_name").focus();</script>'
84+        )
85+       
86+    def testMultiWidgetFirsFieldFocus(self):
87+        """
88+        JavaScript-assisted auto-focus should work if a model/ModelAdmin setup
89+        is such that the first form field has a MultiWidget.
90+        """
91+        response = self.client.get('/test_admin/%s/admin_views/reservation/add/' % self.urlbit)
92+        self.assertContains(
93+            response,
94+            '<script type="text/javascript">document.getElementById("id_start_date_0").focus();</script>'
95+        )
96+
97+
98 class SaveAsTests(TestCase):
99     fixtures = ['admin-views-users.xml','admin-views-person.xml']
100