Ticket #10573: 10573.1.diff

File 10573.1.diff, 3.6 KB (added by Ramiro Morales, 13 years ago)

Alternate Python-based approach

  • 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
    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.stable_id }}").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
    a b  
    522522            return self.html_name
    523523        return ''
    524524    auto_id = property(_auto_id)
     525
     526    def _stable_id(self):
     527        """
     528        Returns a form control ID usable for focusing this field, without
     529        regard to it having a single widget or a MutiWidget.
     530        """
     531        widget = self.field.widget
     532        id_ = widget.attrs.get('id') or self.auto_id
     533        return widget.id_for_label(id_)
     534    stable_id = property(_stable_id)
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    a b  
    611611    owner = models.ForeignKey(User)
    612612    title = models.CharField(max_length=30)
    613613
     614class Reservation(models.Model):
     615    start_date = models.DateTimeField()
     616
    614617admin.site.register(Article, ArticleAdmin)
    615618admin.site.register(CustomArticle, CustomArticleAdmin)
    616619admin.site.register(Section, save_as=True, inlines=[ArticleInline])
     
    642645admin.site.register(PlotDetails)
    643646admin.site.register(CyclicOne)
    644647admin.site.register(CyclicTwo)
     648admin.site.register(Reservation)
    645649
    646650# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    647651# 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
    a b  
    349349        deactivate()
    350350
    351351
     352class AdminJavaScriptTest(AdminViewBasicTest):
     353    def testMultiWidgetFirsFieldFocus(self):
     354        """
     355        JavaScript-assisted auto-focus should work if a model/ModelAdmin setup
     356        is such that the first form field has a MultiWidget.
     357        """
     358        response = self.client.get('/test_admin/%s/admin_views/reservation/add/' % self.urlbit)
     359        self.assertContains(
     360            response,
     361            '<script type="text/javascript">document.getElementById("id_start_date_0").focus();</script>'
     362        )
     363
     364
    352365class SaveAsTests(TestCase):
    353366    fixtures = ['admin-views-users.xml','admin-views-person.xml']
    354367
Back to Top