Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10573 -- Corrected autofocus problem in admin when the first w…

…idget 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
  • Loading branch information...
commit 74ffca17e2ca6a9f061913797efeebbc578692c5 1 parent a105ca5
Russell Keith-Magee authored February 08, 2011
6  django/contrib/admin/templates/admin/change_form.html
@@ -17,8 +17,8 @@
17 17
 {% block breadcrumbs %}{% if not is_popup %}
18 18
 <div class="breadcrumbs">
19 19
      <a href="../../../">{% trans "Home" %}</a> &rsaquo;
20  
-     <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo; 
21  
-     {% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} &rsaquo; 
  20
+     <a href="../../">{{ app_label|capfirst|escape }}</a> &rsaquo;
  21
+     {% if has_change_permission %}<a href="../">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %} &rsaquo;
22 22
      {% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}
23 23
 </div>
24 24
 {% endif %}{% endblock %}
@@ -60,7 +60,7 @@
60 60
 {% submit_row %}
61 61
 
62 62
 {% if adminform and add %}
63  
-   <script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
  63
+   <script type="text/javascript">document.getElementById("{{ adminform.first_field.id_for_label }}").focus();</script>
64 64
 {% endif %}
65 65
 
66 66
 {# JavaScript for prepopulated fields #}
11  django/forms/forms.py
@@ -522,3 +522,14 @@ def _auto_id(self):
522 522
             return self.html_name
523 523
         return ''
524 524
     auto_id = property(_auto_id)
  525
+
  526
+    def _id_for_label(self):
  527
+        """
  528
+        Wrapper around the field widget's `id_for_label` class method.
  529
+        Useful, for example, for focusing on this field regardless of whether
  530
+        it has a single widget or a MutiWidget.
  531
+        """
  532
+        widget = self.field.widget
  533
+        id_ = widget.attrs.get('id') or self.auto_id
  534
+        return widget.id_for_label(id_)
  535
+    id_for_label = property(_id_for_label)
5  tests/regressiontests/admin_views/models.py
@@ -666,6 +666,10 @@ class Answer(models.Model):
666 666
     def __unicode__(self):
667 667
         return self.answer
668 668
 
  669
+class Reservation(models.Model):
  670
+    start_date = models.DateTimeField()
  671
+    price = models.IntegerField()
  672
+    
669 673
 admin.site.register(Article, ArticleAdmin)
670 674
 admin.site.register(CustomArticle, CustomArticleAdmin)
671 675
 admin.site.register(Section, save_as=True, inlines=[ArticleInline])
@@ -701,6 +705,7 @@ def __unicode__(self):
701 705
 admin.site.register(CyclicOne)
702 706
 admin.site.register(CyclicTwo)
703 707
 admin.site.register(WorkHour, WorkHourAdmin)
  708
+admin.site.register(Reservation)
704 709
 
705 710
 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
706 711
 # That way we cover all four cases:
23  tests/regressiontests/admin_views/tests.py
@@ -431,6 +431,29 @@ def test_allowed_filtering_15103(self):
431 431
         except SuspiciousOperation:
432 432
             self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model")
433 433
 
  434
+class AdminJavaScriptTest(AdminViewBasicTest):
  435
+    def testSingleWidgetFirsFieldFocus(self):
  436
+        """
  437
+        JavaScript-assisted auto-focus on first field.
  438
+        """
  439
+        response = self.client.get('/test_admin/%s/admin_views/picture/add/' % self.urlbit)
  440
+        self.assertContains(
  441
+            response,
  442
+            '<script type="text/javascript">document.getElementById("id_name").focus();</script>'
  443
+        )
  444
+        
  445
+    def testMultiWidgetFirsFieldFocus(self):
  446
+        """
  447
+        JavaScript-assisted auto-focus should work if a model/ModelAdmin setup
  448
+        is such that the first form field has a MultiWidget.
  449
+        """
  450
+        response = self.client.get('/test_admin/%s/admin_views/reservation/add/' % self.urlbit)
  451
+        self.assertContains(
  452
+            response,
  453
+            '<script type="text/javascript">document.getElementById("id_start_date_0").focus();</script>'
  454
+        )
  455
+
  456
+
434 457
 class SaveAsTests(TestCase):
435 458
     fixtures = ['admin-views-users.xml','admin-views-person.xml']
436 459
 

0 notes on commit 74ffca1

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