pythonflaskhttp-redirectreferrer

Flask go back to the previous previous url (go back twice)


I am using flask for a recipe app. When a user edits a recipe, I'd like to go back to where they came from when they click "update" and submit the form. Since the referrer of the request is the "editrecipe" url, I need to find a way to send them back twice. This answer helps me to go back once, but ideally I'd like to do something like this:

redirect_url = request.args.get('previous')[-2]

OR

redirect_url = request.referrer(-2)

Neither of these work (for obvious reasons), I was just wondering if there's a way to access the browser history and go back twice?


Solution

  • So, you can try something like this.

    The basic idea here to have a after request handler and push the visited URIs to your session's history. Now, when you want to redirect, then get the history variable from flask-session and simply redirect to the last_before value.