1
0
mirror of https://github.com/django/django.git synced 2025-09-24 23:49:12 +00:00

[6.0.x] Fixed #36488 -- Fixed merging of query strings in RedirectView.

Co-authored-by: Ethan Jucovy <ethan.jucovy@gmail.com>
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>

Backport of a36df6890d8995480f2e95ba556b77cef975d4f6 from main.
This commit is contained in:
Samriddha9619 2025-09-08 18:26:32 +05:30 committed by Sarah Boyce
parent 77ae09916d
commit 359c1c6ff9
2 changed files with 30 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import logging
from urllib.parse import urlparse
from asgiref.sync import iscoroutinefunction, markcoroutinefunction
@ -252,7 +253,10 @@ class RedirectView(View):
args = self.request.META.get("QUERY_STRING", "")
if args and self.query_string:
url = "%s?%s" % (url, args)
if urlparse(url).query:
url = f"{url}&{args}"
else:
url = f"{url}?{args}"
return url
def get(self, request, *args, **kwargs):

View File

@ -587,6 +587,31 @@ class RedirectViewTest(LoggingAssertionMixin, SimpleTestCase):
handler, f"Gone: {escaped}", logging.WARNING, 410, request
)
def test_redirect_with_querry_string_in_destination(self):
response = RedirectView.as_view(url="/bar/?pork=spam", query_string=True)(
self.rf.get("/foo")
)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.headers["Location"], "/bar/?pork=spam")
def test_redirect_with_query_string_in_destination_and_request(self):
response = RedirectView.as_view(url="/bar/?pork=spam", query_string=True)(
self.rf.get("/foo/?utm_source=social")
)
self.assertEqual(response.status_code, 302)
self.assertEqual(
response.headers["Location"], "/bar/?pork=spam&utm_source=social"
)
def test_redirect_with_same_query_string_param_will_append_not_replace(self):
response = RedirectView.as_view(url="/bar/?pork=spam", query_string=True)(
self.rf.get("/foo/?utm_source=social&pork=ham")
)
self.assertEqual(response.status_code, 302)
self.assertEqual(
response.headers["Location"], "/bar/?pork=spam&utm_source=social&pork=ham"
)
class GetContextDataTest(SimpleTestCase):
def test_get_context_data_super(self):