diff --git a/django/contrib/admin/templates/admin/edit_inline/stacked.html b/django/contrib/admin/templates/admin/edit_inline/stacked.html index ccc7134c7b3..457e84affe5 100644 --- a/django/contrib/admin/templates/admin/edit_inline/stacked.html +++ b/django/contrib/admin/templates/admin/edit_inline/stacked.html @@ -12,7 +12,7 @@ {{ inline_admin_formset.formset.management_form }} {{ inline_admin_formset.formset.non_form_errors }} -{% for inline_admin_form in inline_admin_formset %}
+{% for inline_admin_form in inline_admin_formset %}

{{ inline_admin_formset.opts.verbose_name|capfirst }}: {% if inline_admin_form.original %}{{ inline_admin_form.original }}{% if inline_admin_form.model_admin.show_change_link and inline_admin_form.model_admin.has_registered_model %} {% if inline_admin_formset.has_change_permission %}{% translate "Change" %}{% else %}{% translate "View" %}{% endif %}{% endif %} {% else %}#{{ forloop.counter }}{% endif %} {% if inline_admin_form.show_url %}{% translate "View on site" %}{% endif %} diff --git a/django/contrib/admin/templates/admin/edit_inline/tabular.html b/django/contrib/admin/templates/admin/edit_inline/tabular.html index 4c0a08cbfca..6b6bd1b4cf0 100644 --- a/django/contrib/admin/templates/admin/edit_inline/tabular.html +++ b/django/contrib/admin/templates/admin/edit_inline/tabular.html @@ -28,7 +28,7 @@ {{ inline_admin_form.form.non_field_errors }} {% endif %} + id="{{ inline_admin_formset.formset.prefix }}-{% if forloop.last and inline_admin_formset.has_add_permission %}empty{% else %}{{ forloop.counter0 }}{% endif %}"> {% if inline_admin_form.original or inline_admin_form.show_url %}

{% if inline_admin_form.original %} diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 145add114cf..11b8aff346d 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -2694,6 +2694,20 @@ class AdminViewPermissionsTest(TestCase): formset = response.context["inline_admin_formsets"][0] self.assertEqual(len(formset.forms), 3) + def test_change_view_with_view_only_last_inline(self): + self.viewuser.user_permissions.add( + get_perm(Section, get_permission_codename("view", Section._meta)) + ) + self.client.force_login(self.viewuser) + response = self.client.get( + reverse("admin:admin_views_section_change", args=(self.s1.pk,)) + ) + self.assertEqual(len(response.context["inline_admin_formsets"]), 1) + formset = response.context["inline_admin_formsets"][0] + self.assertEqual(len(formset.forms), 3) + # The last inline is not marked as empty. + self.assertContains(response, 'id="article_set-2"') + def test_change_view_with_view_and_add_inlines(self): """User has view and add permissions on the inline model.""" self.viewuser.user_permissions.add(