Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.2.X] 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.

Backport of r15452 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15456 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 27e7fcbab015452cb4201f32eb2682bd8dc23feb 1 parent 09a8205
@freakboy3742 freakboy3742 authored
View
6 django/contrib/admin/templates/admin/change_form.html
@@ -16,8 +16,8 @@
{% block breadcrumbs %}{% if not is_popup %}
<div class="breadcrumbs">
<a href="../../../">{% trans "Home" %}</a> &rsaquo;
- <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
- {% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} &rsaquo;
+ <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
+ {% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} &rsaquo;
{% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}
</div>
{% endif %}{% endblock %}
@@ -56,7 +56,7 @@
{% submit_row %}
{% if adminform and add %}
- <script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
+ <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
{% endif %}
{# JavaScript for prepopulated fields #}
View
11 django/forms/forms.py
@@ -518,3 +518,14 @@ def _auto_id(self):
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)
View
5 tests/regressiontests/admin_views/models.py
@@ -629,6 +629,10 @@ class WorkHourAdmin(admin.ModelAdmin):
list_display = ('datum', 'employee')
list_filter = ('employee',)
+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])
@@ -664,6 +668,7 @@ class WorkHourAdmin(admin.ModelAdmin):
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:
View
23 tests/regressiontests/admin_views/tests.py
@@ -376,6 +376,29 @@ def test_allowed_filtering_15103(self):
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,
+ '<script type="text/javascript">document.getElementById("id_name").focus();</script>'
+ )
+
+ 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,
+ '<script type="text/javascript">document.getElementById("id_start_date_0").focus();</script>'
+ )
+
+
class SaveAsTests(TestCase):
fixtures = ['admin-views-users.xml','admin-views-person.xml']

0 comments on commit 27e7fcb

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