Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The secure redirect I found in the snippets library did not satisfy me.
- # Here is my version which satisfied my needs more. The next argument/
- # referrer will only be used if your application knows how to route it.
- # This won't work very well if you have multiple applications.
- # Licensed under the Unlicense, go nuts.
- ## Create the base form ##
- from flask import request, url_for, render_template
- from flaskext.wtf import Form, HiddenField
- from werkzeug.exceptions import NotFound, HTTPException
- class RedirectForm(Form):
- next = HiddenField()
- def __init__(self, *args, **kwargs):
- super(RedirectForm, self).__init__(*args, **kwargs)
- if not self.next.data:
- self.next.data = request.args.get("next") or request.referrer
- try:
- # Will raise an exception if no endpoint exists for the url
- app.create_url_adapter(request).match(self.next.data)
- except NotFound:
- self.next.data = ""
- except HTTPException:
- # Any other exceptions are harmless (I think)
- pass
- @property
- def redirect_target(self):
- return self.next.data
Add Comment
Please, Sign In to add comment