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 /docs | |
| 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 'docs')
| -rw-r--r-- | docs/ref/request-response.txt | 18 | ||||
| -rw-r--r-- | docs/releases/5.2.txt | 10 | ||||
| -rw-r--r-- | docs/topics/http/shortcuts.txt | 36 |
3 files changed, 59 insertions, 5 deletions
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index afebd00d8b..26fcb5fa08 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -1070,18 +1070,32 @@ types of HTTP responses. Like ``HttpResponse``, these subclasses live in (e.g. ``'https://www.yahoo.com/search/'``), an absolute path with no domain (e.g. ``'/search/'``), or even a relative path (e.g. ``'search/'``). In that last case, the client browser will reconstruct the full URL itself - according to the current path. See :class:`HttpResponse` for other optional - constructor arguments. Note that this returns an HTTP status code 302. + according to the current path. + + The constructor accepts an optional ``preserve_request`` keyword argument + that defaults to ``False``, producing a response with a 302 status code. If + ``preserve_request`` is ``True``, the status code will be 307 instead. + + See :class:`HttpResponse` for other optional constructor arguments. .. attribute:: HttpResponseRedirect.url This read-only attribute represents the URL the response will redirect to (equivalent to the ``Location`` response header). + .. versionchanged:: 5.2 + + The ``preserve_request`` argument was added. + .. class:: HttpResponsePermanentRedirect Like :class:`HttpResponseRedirect`, but it returns a permanent redirect (HTTP status code 301) instead of a "found" redirect (status code 302). + When ``preserve_request=True``, the response's status code is 308. + + .. versionchanged:: 5.2 + + The ``preserve_request`` argument was added. .. class:: HttpResponseNotModified diff --git a/docs/releases/5.2.txt b/docs/releases/5.2.txt index 88a1daa45d..0ee4868246 100644 --- a/docs/releases/5.2.txt +++ b/docs/releases/5.2.txt @@ -294,6 +294,16 @@ Requests and Responses * The new :meth:`.HttpRequest.get_preferred_type` method can be used to query the preferred media type the client accepts. +* The new ``preserve_request`` argument for + :class:`~django.http.HttpResponseRedirect` and + :class:`~django.http.HttpResponsePermanentRedirect` + determines whether the HTTP status codes 302/307 or 301/308 are used, + respectively. + +* The new ``preserve_request`` argument for + :func:`~django.shortcuts.redirect` allows to instruct the user agent to reuse + the HTTP method and body during redirection using specific status codes. + Security ~~~~~~~~ diff --git a/docs/topics/http/shortcuts.txt b/docs/topics/http/shortcuts.txt index 171cfc3c93..308eae0855 100644 --- a/docs/topics/http/shortcuts.txt +++ b/docs/topics/http/shortcuts.txt @@ -91,7 +91,7 @@ This example is equivalent to:: ``redirect()`` ============== -.. function:: redirect(to, *args, permanent=False, **kwargs) +.. function:: redirect(to, *args, permanent=False, preserve_request=False, **kwargs) Returns an :class:`~django.http.HttpResponseRedirect` to the appropriate URL for the arguments passed. @@ -107,8 +107,27 @@ This example is equivalent to:: * An absolute or relative URL, which will be used as-is for the redirect location. - By default issues a temporary redirect; pass ``permanent=True`` to issue a - permanent redirect. + By default, a temporary redirect is issued with a 302 status code. If + ``permanent=True``, a permanent redirect is issued with a 301 status code. + + If ``preserve_request=True``, the response instructs the user agent to + preserve the method and body of the original request when issuing the + redirect. In this case, temporary redirects use a 307 status code, and + permanent redirects use a 308 status code. This is better illustrated in the + following table: + + ========= ================ ================ + permanent preserve_request HTTP status code + ========= ================ ================ + ``True`` ``False`` 301 + ``False`` ``False`` 302 + ``False`` ``True`` 307 + ``True`` ``True`` 308 + ========= ================ ================ + + .. versionchanged:: 5.2 + + The argument ``preserve_request`` was added. Examples -------- @@ -158,6 +177,17 @@ will be returned:: obj = MyModel.objects.get(...) return redirect(obj, permanent=True) +Additionally, the ``preserve_request`` argument can be used to preserve the +original HTTP method:: + + def my_view(request): + # ... + obj = MyModel.objects.get(...) + if request.method in ("POST", "PUT"): + # Redirection preserves the original request method. + return redirect(obj, preserve_request=True) + # ... + ``get_object_or_404()`` ======================= |
