From 1f193f7f56d0f83c188dca8b03266fbaa339d0f5 Mon Sep 17 00:00:00 2001 From: Bakdolot Date: Wed, 8 Feb 2023 00:03:24 +0600 Subject: [PATCH] [4.2.x] Fixed #34315 -- Preserved admin changelist filters on "Close" button. Backport of 325c44ac6c070465ef2b3b7b8ed06cbcb88a3f10 from main --- .../admin/templates/admin/submit_line.html | 5 ++++- tests/admin_views/tests.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/templates/admin/submit_line.html b/django/contrib/admin/templates/admin/submit_line.html index a1082ed1965..b2b20549661 100644 --- a/django/contrib/admin/templates/admin/submit_line.html +++ b/django/contrib/admin/templates/admin/submit_line.html @@ -5,7 +5,10 @@ {% if show_save_as_new %}{% endif %} {% if show_save_and_add_another %}{% endif %} {% if show_save_and_continue %}{% endif %} -{% if show_close %}{% translate 'Close' %}{% endif %} +{% if show_close %} + {% url opts|admin_urlname:'changelist' as changelist_url %} + {% translate 'Close' %} +{% endif %} {% if show_delete_link and original %} {% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %} {% translate "Delete" %} diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index 752b46b7578..548444390fc 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -7949,6 +7949,21 @@ class AdminKeepChangeListFiltersTests(TestCase): self.assertRedirects(response, self.get_add_url()) post_data.pop("_addanother") + def test_change_view_close_link(self): + viewuser = User.objects.create_user( + username="view", password="secret", is_staff=True + ) + viewuser.user_permissions.add( + get_perm(User, get_permission_codename("view", User._meta)) + ) + self.client.force_login(viewuser) + response = self.client.get(self.get_change_url()) + close_link = re.search( + 'Close', response.content.decode() + ) + close_link = close_link[1].replace("&", "&") + self.assertURLEqual(close_link, self.get_changelist_url()) + def test_change_view_without_preserved_filters(self): response = self.client.get(self.get_change_url(add_preserved_filters=False)) # The action attribute is omitted.