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 ba31b01034.
This commit is contained in:
Dominique Bischof 2023-03-02 14:03:50 -05:00 committed by Mariusz Felisiak
parent 4338b6526d
commit 17e08b2177
2 changed files with 39 additions and 1 deletions

View File

@ -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):

View File

@ -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(