Skip to content
Advertisement

Flask 302 error code when using flask.redirect

I have this code https://github.com/italomaia/flask-empty/blob/master/src/0.8/main.py and I wrote at the end of the file:

JavaScript

If I run this code, I get 302 server error (ERR_TOO_MANY_REDIRECTS), but if I change this line return redirect(url_for('login')) by return 'Hello!' it works without errors! What am I doing wrong?

Advertisement

Answer

Well I am not a specialist on flask. But obviously you are using a signal before the request gets mapped to a handler to check for credentials and then redirect to a handler. But the redirect in turn will trigger another request to your app and invoke the same function again, sending you into an infinite redirect loop. (Error 302 is a specific http error for this situation)

My advice: Check for credentials on per handler function basis or make at least an exception to your before_request function that it doesn’t get invoked when a request to “login/” occurs.

It might also be possible to directly invoke the function that handles login/

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement