From 17e08b2177928b02158997ee1c44d8322a2e6eb8 Mon Sep 17 00:00:00 2001 From: Dominique Bischof Date: Thu, 2 Mar 2023 14:03:50 -0500 Subject: [PATCH] Fixed #34377 -- Fixed preserving query strings in AdminSite.catch_all_view(). Included full path when redirecting with append slash to include query strings. Regression in ba31b0103442ac891fb3cb98f316781254e366c3. --- django/contrib/admin/sites.py | 4 +++- tests/admin_views/tests.py | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 61be31d890..3ff7088506 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -453,7 +453,9 @@ class AdminSite: pass else: if getattr(match.func, "should_append_slash", True): - return HttpResponsePermanentRedirect("%s/" % request.path) + return HttpResponsePermanentRedirect( + request.get_full_path(force_append_slash=True) + ) raise Http404 def _build_app_dict(self, request, label=None): diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py index f1d098eb40..f440b85b6e 100644 --- a/tests/admin_views/tests.py +++ b/tests/admin_views/tests.py @@ -8463,6 +8463,24 @@ class AdminSiteFinalCatchAllPatternTests(TestCase): response, known_url, status_code=301, target_status_code=403 ) + @override_settings(APPEND_SLASH=True) + def test_missing_slash_append_slash_true_query_string(self): + superuser = User.objects.create_user( + username="staff", + password="secret", + email="staff@example.com", + is_staff=True, + ) + self.client.force_login(superuser) + known_url = reverse("admin:admin_views_article_changelist") + response = self.client.get("%s?id=1" % known_url[:-1]) + self.assertRedirects( + response, + f"{known_url}?id=1", + status_code=301, + fetch_redirect_response=False, + ) + @override_settings(APPEND_SLASH=True) def test_missing_slash_append_slash_true_script_name(self): superuser = User.objects.create_user( @@ -8481,6 +8499,24 @@ class AdminSiteFinalCatchAllPatternTests(TestCase): fetch_redirect_response=False, ) + @override_settings(APPEND_SLASH=True) + def test_missing_slash_append_slash_true_script_name_query_string(self): + superuser = User.objects.create_user( + username="staff", + password="secret", + email="staff@example.com", + is_staff=True, + ) + self.client.force_login(superuser) + known_url = reverse("admin:admin_views_article_changelist") + response = self.client.get("%s?id=1" % known_url[:-1], SCRIPT_NAME="/prefix/") + self.assertRedirects( + response, + f"/prefix{known_url}?id=1", + status_code=301, + fetch_redirect_response=False, + ) + @override_settings(APPEND_SLASH=True, FORCE_SCRIPT_NAME="/prefix/") def test_missing_slash_append_slash_true_force_script_name(self): superuser = User.objects.create_user(