diff options
| author | Lorenzo Peña <lorinkoz@gmail.com> | 2024-11-14 19:53:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-14 15:53:49 -0300 |
| commit | 91c879eda595c12477bbfa6f51115e88b75ddf88 (patch) | |
| tree | 544ce7be64975158b6d3f0d0a8ea693ab7188aca /django | |
| parent | 8590d05d44a4f3df56d988229e43d66c37df79da (diff) | |
Fixed #35784 -- Added support for preserving the HTTP request method in HttpResponseRedirectBase.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Diffstat (limited to 'django')
| -rw-r--r-- | django/http/response.py | 6 | ||||
| -rw-r--r-- | django/shortcuts.py | 12 |
2 files changed, 13 insertions, 5 deletions
diff --git a/django/http/response.py b/django/http/response.py index 1dbaf46add..4a0ea67013 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -627,10 +627,12 @@ class FileResponse(StreamingHttpResponse): class HttpResponseRedirectBase(HttpResponse): allowed_schemes = ["http", "https", "ftp"] - def __init__(self, redirect_to, *args, **kwargs): + def __init__(self, redirect_to, preserve_request=False, *args, **kwargs): super().__init__(*args, **kwargs) self["Location"] = iri_to_uri(redirect_to) parsed = urlsplit(str(redirect_to)) + if preserve_request: + self.status_code = self.status_code_preserve_request if parsed.scheme and parsed.scheme not in self.allowed_schemes: raise DisallowedRedirect( "Unsafe redirect to URL with protocol '%s'" % parsed.scheme @@ -652,10 +654,12 @@ class HttpResponseRedirectBase(HttpResponse): class HttpResponseRedirect(HttpResponseRedirectBase): status_code = 302 + status_code_preserve_request = 307 class HttpResponsePermanentRedirect(HttpResponseRedirectBase): status_code = 301 + status_code_preserve_request = 308 class HttpResponseNotModified(HttpResponse): diff --git a/django/shortcuts.py b/django/shortcuts.py index b8b5be1f5f..6274631dba 100644 --- a/django/shortcuts.py +++ b/django/shortcuts.py @@ -26,7 +26,7 @@ def render( return HttpResponse(content, content_type, status) -def redirect(to, *args, permanent=False, **kwargs): +def redirect(to, *args, permanent=False, preserve_request=False, **kwargs): """ Return an HttpResponseRedirect to the appropriate URL for the arguments passed. @@ -40,13 +40,17 @@ def redirect(to, *args, permanent=False, **kwargs): * A URL, which will be used as-is for the redirect location. - Issues a temporary redirect by default; pass permanent=True to issue a - permanent redirect. + Issues a temporary redirect by default. Set permanent=True to issue a + permanent redirect. Set preserve_request=True to instruct the user agent + to preserve the original HTTP method and body when following the redirect. """ redirect_class = ( HttpResponsePermanentRedirect if permanent else HttpResponseRedirect ) - return redirect_class(resolve_url(to, *args, **kwargs)) + return redirect_class( + resolve_url(to, *args, **kwargs), + preserve_request=preserve_request, + ) def _get_queryset(klass): |
