Code

Ticket #10573: admin.diff

File admin.diff, 1.5 KB (added by rduffield, 5 years ago)
Line 
1Index: django/forms/forms.py
2===================================================================
3--- django/forms/forms.py       (revision 10105)
4+++ django/forms/forms.py       (working copy)
5@@ -10,7 +10,7 @@
6 from django.utils.safestring import mark_safe
7 
8 from fields import Field, FileField
9-from widgets import Media, media_property, TextInput, Textarea
10+from widgets import Media, media_property, TextInput, Textarea, MultiWidget
11 from util import flatatt, ErrorDict, ErrorList, ValidationError
12 
13 __all__ = ('BaseForm', 'Form')
14@@ -434,3 +434,13 @@
15             return self.html_name
16         return ''
17     auto_id = property(_auto_id)
18+
19+    def _focus_id(self):
20+        """
21+        Returns the correct field to auto-focus on when rendering the form.
22+        """
23+        if isinstance(self.field.widget, MultiWidget):
24+            return '%s_0' % self.auto_id
25+        else:
26+            return self.auto_id
27+    focus_id = property(_focus_id)
28Index: django/contrib/admin/templates/admin/change_form.html
29===================================================================
30--- django/contrib/admin/templates/admin/change_form.html       (revision 10105)
31+++ django/contrib/admin/templates/admin/change_form.html       (working copy)
32@@ -55,7 +55,7 @@
33 {% submit_row %}
34 
35 {% if add %}
36-   <script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
37+   <script type="text/javascript">document.getElementById("{{ adminform.first_field.focus_id }}").focus();</script>
38 {% endif %}
39 
40 {# JavaScript for prepopulated fields #}