diff options
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): |
