Redirect back in Flask

后端 未结 1 1825
猫巷女王i
猫巷女王i 2020-12-05 12:05

I have a DB Table called Item. Item has a status attribute, that can be either of

new
todo
doing
done

On my website I have two

相关标签:
1条回答
  • 2020-12-05 12:31

    I'm using the helper function which is recommended here: http://flask.pocoo.org/docs/reqcontext/

    def redirect_url(default='index'):
        return request.args.get('next') or \
               request.referrer or \
               url_for(default)
    

    Use it in in the view

    def some_view():
        # some action
        return redirect(redirect_url())
    

    Without any parameters it will redirect the user back to where he came from (request.referrer). You can add the get parameter next to specify a url. This is useful for oauth for example.

    instagram.authorize(callback=url_for(".oauth_authorized",
                                                    next=redirect_url(),
                                                    _external=True))
    

    I also added a default view if there should be no referrer for some reason

    redirect_url('.another_view')

    The snippet you linked basically does the same but more secure by ensuring you cannot get redirected to a malicious attacker's page on another host.

    0 讨论(0)
提交回复
热议问题