Skip to content

Commit eca5fd1

Browse files
committed
redirect defaults to 303
1 parent eb58d86 commit eca5fd1

4 files changed

Lines changed: 14 additions & 3 deletions

File tree

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ Unreleased
1616
deprecation period. :issue:`5815`
1717
- ``template_filter``, ``template_test``, and ``template_global`` decorators
1818
can be used without parentheses. :issue:`5729`
19+
- ``redirect`` returns a ``303`` status code by default instead of ``302``.
20+
This tells the client to always switch to ``GET``, rather than only
21+
switching ``POST`` to ``GET``. This preserves the current behavior of
22+
``GET`` and ``POST`` redirects, and is also correct for frontend libraries
23+
such as HTMX. :issue:`5895`
1924

2025

2126
Version 3.1.2

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ This specifies that ``/users/`` will be the URL for page one and
596596
``/users/page/N`` will be the URL for page ``N``.
597597

598598
If a URL contains a default value, it will be redirected to its simpler
599-
form with a 301 redirect. In the above example, ``/users/page/1`` will
599+
form with a 308 redirect. In the above example, ``/users/page/1`` will
600600
be redirected to ``/users/``. If your route handles ``GET`` and ``POST``
601601
requests, make sure the default route only handles ``GET``, as redirects
602602
can't preserve form data. ::

src/flask/helpers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def url_for(
239239

240240

241241
def redirect(
242-
location: str, code: int = 302, Response: type[BaseResponse] | None = None
242+
location: str, code: int = 303, Response: type[BaseResponse] | None = None
243243
) -> BaseResponse:
244244
"""Create a redirect response object.
245245
@@ -252,6 +252,9 @@ def redirect(
252252
:param Response: The response class to use. Not used when
253253
``current_app`` is active, which uses ``app.response_class``.
254254
255+
.. versionchanged:: 3.2
256+
``code`` defaults to ``303`` instead of ``302``.
257+
255258
.. versionadded:: 2.2
256259
Calls ``current_app.redirect`` if available instead of always
257260
using Werkzeug's default ``redirect``.

src/flask/sansio/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ def should_ignore_error(self, error: BaseException | None) -> bool:
932932
"""
933933
return False
934934

935-
def redirect(self, location: str, code: int = 302) -> BaseResponse:
935+
def redirect(self, location: str, code: int = 303) -> BaseResponse:
936936
"""Create a redirect response object.
937937
938938
This is called by :func:`flask.redirect`, and can be called
@@ -941,6 +941,9 @@ def redirect(self, location: str, code: int = 302) -> BaseResponse:
941941
:param location: The URL to redirect to.
942942
:param code: The status code for the redirect.
943943
944+
.. versionchanged:: 3.2
945+
``code`` defaults to ``303`` instead of ``302``.
946+
944947
.. versionadded:: 2.2
945948
Moved from ``flask.redirect``, which calls this method.
946949
"""

0 commit comments

Comments
 (0)