Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a redirect with code 401 from Flask doesn't redirect

When i do this, it work:

return redirect('/admin-login')

But when I try to pass a http code in parameter, the redirection dont work and I get to this page:`

return redirect('/admin-login', code=401)

Redirecting...

You should be redirected automatically to target URL: /admin-login. If not click the link.

How do I fix this? Thanks

like image 450
QuebecSquad Avatar asked Dec 07 '25 15:12

QuebecSquad


1 Answers

The message is actually there in both cases. Flask uses the Location header field to trigger a redirect. So when your browser sees that it skips displaying the content and immediately redirects.

The reason it doesn't work for code=401, is because 401 Unauthorized is a client error. This results in the browser ignoring the redirect (at least with Chrome).

The Flask documentation for flask.redirect mentions the following:

Supported codes are 301, 302, 303, 305, and 307.

It doesn't specifically mention why they are the only supported ones. But the best assumption is because browsers generally shouldn't redirect if it's not a 3XX status code (Redirection).

The solution is thus to use one of the supported status codes.

like image 112
vallentin Avatar answered Dec 09 '25 06:12

vallentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!