1
0
mirror of https://github.com/django/django.git synced 2025-09-25 07:59:11 +00:00

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>
This commit is contained in:
Samriddha9619 2025-09-08 18:26:32 +05:30 committed by Sarah Boyce
parent 336e713e2a
commit a36df6890d
2 changed files with 30 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import logging import logging
from urllib.parse import urlparse
from asgiref.sync import iscoroutinefunction, markcoroutinefunction from asgiref.sync import iscoroutinefunction, markcoroutinefunction
@ -252,7 +253,10 @@ class RedirectView(View):
args = self.request.META.get("QUERY_STRING", "") args = self.request.META.get("QUERY_STRING", "")
if args and self.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 return url
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):

View File

@ -587,6 +587,31 @@ class RedirectViewTest(LoggingAssertionMixin, SimpleTestCase):
handler, f"Gone: {escaped}", logging.WARNING, 410, request 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): class GetContextDataTest(SimpleTestCase):
def test_get_context_data_super(self): def test_get_context_data_super(self):