Ticket #10573: 10573.2.diff

File 10573.2.diff, 4.4 KB (added by Julien Phalip, 14 years ago)
  • django/contrib/admin/templates/admin/change_form.html

    diff --git a/django/contrib/admin/templates/admin/change_form.html b/django/contrib/admin/templates/admin/change_form.html
    index 8f8dfe6..fb278b4 100644
    a b  
    1717{% block breadcrumbs %}{% if not is_popup %}
    1818<div class="breadcrumbs">
    1919     <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;
    2222     {% if add %}{% trans "Add" %} {{ opts.verbose_name }}{% else %}{{ original|truncatewords:"18" }}{% endif %}
    2323</div>
    2424{% endif %}{% endblock %}
     
    6060{% submit_row %}
    6161
    6262{% 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>
    6464{% endif %}
    6565
    6666{# JavaScript for prepopulated fields #}
  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index 1cf6f2a..f776861 100644
    a b class BoundField(StrAndUnicode):  
    522522            return self.html_name
    523523        return ''
    524524    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)
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 02c6b92..60319ea 100644
    a b class Answer(models.Model):  
    666666    def __unicode__(self):
    667667        return self.answer
    668668
     669class Reservation(models.Model):
     670    start_date = models.DateTimeField()
     671    price = models.IntegerField()
     672   
    669673admin.site.register(Article, ArticleAdmin)
    670674admin.site.register(CustomArticle, CustomArticleAdmin)
    671675admin.site.register(Section, save_as=True, inlines=[ArticleInline])
    admin.site.register(PlotDetails)  
    701705admin.site.register(CyclicOne)
    702706admin.site.register(CyclicTwo)
    703707admin.site.register(WorkHour, WorkHourAdmin)
     708admin.site.register(Reservation)
    704709
    705710# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    706711# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index f392393..d4d58a2 100644
    a b class AdminViewBasicTest(TestCase):  
    431431        except SuspiciousOperation:
    432432            self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model")
    433433
     434class 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
    434457class SaveAsTests(TestCase):
    435458    fixtures = ['admin-views-users.xml','admin-views-person.xml']
    436459
Back to Top