redirect defaults to 303 (#5898)

This commit is contained in:
David Lord 2026-01-24 17:18:35 -08:00 committed by GitHub
commit 809d5a8869
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 3 deletions

View file

@ -16,6 +16,11 @@ Unreleased
deprecation period. :issue:`5815` deprecation period. :issue:`5815`
- ``template_filter``, ``template_test``, and ``template_global`` decorators - ``template_filter``, ``template_test``, and ``template_global`` decorators
can be used without parentheses. :issue:`5729` can be used without parentheses. :issue:`5729`
- ``redirect`` returns a ``303`` status code by default instead of ``302``.
This tells the client to always switch to ``GET``, rather than only
switching ``POST`` to ``GET``. This preserves the current behavior of
``GET`` and ``POST`` redirects, and is also correct for frontend libraries
such as HTMX. :issue:`5895`
Version 3.1.2 Version 3.1.2

View file

@ -596,7 +596,7 @@ This specifies that ``/users/`` will be the URL for page one and
``/users/page/N`` will be the URL for page ``N``. ``/users/page/N`` will be the URL for page ``N``.
If a URL contains a default value, it will be redirected to its simpler If a URL contains a default value, it will be redirected to its simpler
form with a 301 redirect. In the above example, ``/users/page/1`` will form with a 308 redirect. In the above example, ``/users/page/1`` will
be redirected to ``/users/``. If your route handles ``GET`` and ``POST`` be redirected to ``/users/``. If your route handles ``GET`` and ``POST``
requests, make sure the default route only handles ``GET``, as redirects requests, make sure the default route only handles ``GET``, as redirects
can't preserve form data. :: can't preserve form data. ::

View file

@ -239,7 +239,7 @@ def url_for(
def redirect( def redirect(
location: str, code: int = 302, Response: type[BaseResponse] | None = None location: str, code: int = 303, Response: type[BaseResponse] | None = None
) -> BaseResponse: ) -> BaseResponse:
"""Create a redirect response object. """Create a redirect response object.
@ -252,6 +252,9 @@ def redirect(
:param Response: The response class to use. Not used when :param Response: The response class to use. Not used when
``current_app`` is active, which uses ``app.response_class``. ``current_app`` is active, which uses ``app.response_class``.
.. versionchanged:: 3.2
``code`` defaults to ``303`` instead of ``302``.
.. versionadded:: 2.2 .. versionadded:: 2.2
Calls ``current_app.redirect`` if available instead of always Calls ``current_app.redirect`` if available instead of always
using Werkzeug's default ``redirect``. using Werkzeug's default ``redirect``.

View file

@ -932,7 +932,7 @@ class App(Scaffold):
""" """
return False return False
def redirect(self, location: str, code: int = 302) -> BaseResponse: def redirect(self, location: str, code: int = 303) -> BaseResponse:
"""Create a redirect response object. """Create a redirect response object.
This is called by :func:`flask.redirect`, and can be called This is called by :func:`flask.redirect`, and can be called
@ -941,6 +941,9 @@ class App(Scaffold):
:param location: The URL to redirect to. :param location: The URL to redirect to.
:param code: The status code for the redirect. :param code: The status code for the redirect.
.. versionchanged:: 3.2
``code`` defaults to ``303`` instead of ``302``.
.. versionadded:: 2.2 .. versionadded:: 2.2
Moved from ``flask.redirect``, which calls this method. Moved from ``flask.redirect``, which calls this method.
""" """