Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Unreleased
deprecation period. :issue:`5815`
- ``template_filter``, ``template_test``, and ``template_global`` decorators
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
Expand Down
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

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``
requests, make sure the default route only handles ``GET``, as redirects
can't preserve form data. ::
Expand Down
5 changes: 4 additions & 1 deletion src/flask/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def url_for(


def redirect(
location: str, code: int = 302, Response: type[BaseResponse] | None = None
location: str, code: int = 303, Response: type[BaseResponse] | None = None
) -> BaseResponse:
"""Create a redirect response object.

Expand All @@ -252,6 +252,9 @@ def redirect(
:param Response: The response class to use. Not used when
``current_app`` is active, which uses ``app.response_class``.

.. versionchanged:: 3.2
``code`` defaults to ``303`` instead of ``302``.

.. versionadded:: 2.2
Calls ``current_app.redirect`` if available instead of always
using Werkzeug's default ``redirect``.
Expand Down
5 changes: 4 additions & 1 deletion src/flask/sansio/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ def should_ignore_error(self, error: BaseException | None) -> bool:
"""
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.

This is called by :func:`flask.redirect`, and can be called
Expand All @@ -941,6 +941,9 @@ def redirect(self, location: str, code: int = 302) -> BaseResponse:
:param location: The URL to redirect to.
:param code: The status code for the redirect.

.. versionchanged:: 3.2
``code`` defaults to ``303`` instead of ``302``.

.. versionadded:: 2.2
Moved from ``flask.redirect``, which calls this method.
"""
Expand Down