|
| 1 | +title: flask redirect Example Python Code |
| 2 | +category: page |
| 3 | +slug: flask-redirect-examples |
| 4 | +sortorder: 50104 |
| 5 | +toc: False |
| 6 | +sidebartitle: flask redirect |
| 7 | +meta: Python code examples for the redirect function from the Flask project used for building web applications. |
| 8 | + |
| 9 | + |
| 10 | +The [Flask](/flask.html) |
| 11 | +[redirect](https://flask.palletsprojects.com/en/1.1.x/api/#flask.redirect) |
| 12 | +([source code](https://github.com/pallets/werkzeug/blob/master/src/werkzeug/utils.py)) |
| 13 | +function appropriately sends a redirect status code, one of |
| 14 | +301, 302, 303, 305, 307, or 308. |
| 15 | + |
| 16 | + |
| 17 | +## Example 1 from FlaskBB |
| 18 | +[FlaskBB](https://github.com/flaskbb/flaskbb) |
| 19 | +([project website](https://flaskbb.org/)) is a [Flask](/flask.html)-based |
| 20 | +forum web application. The web app allows users to chat in an open |
| 21 | +message board or send private messages in plain text or |
| 22 | +[Markdown](/markdown.html). FlaskBB is provided as open source |
| 23 | +[under this license](https://github.com/flaskbb/flaskbb/blob/master/LICENSE). |
| 24 | + |
| 25 | +This example can be slightly confusing because it wraps `redirect` and calls |
| 26 | +`self.redirect` to invoke the wrapper function. |
| 27 | + |
| 28 | +[**flaskbb / flaskbb / user / views.py**](https://github.com/flaskbb/flaskbb/blob/master/flaskbb/user/views.py) |
| 29 | + |
| 30 | +```python |
| 31 | +# -*- coding: utf-8 -*- |
| 32 | +""" |
| 33 | + flaskbb.user.views |
| 34 | + ------------------ |
| 35 | + The user view handles the user profile |
| 36 | + and the user settings from a signed in user. |
| 37 | + :copyright: (c) 2014 by the FlaskBB Team. |
| 38 | + :license: BSD, see LICENSE for more details. |
| 39 | +""" |
| 40 | +import logging |
| 41 | + |
| 42 | +import attr |
| 43 | +~~from flask import Blueprint, flash, redirect, request, url_for |
| 44 | +from flask.views import MethodView |
| 45 | +from flask_babelplus import gettext as _ |
| 46 | +from flask_login import current_user, login_required |
| 47 | +from pluggy import HookimplMarker |
| 48 | + |
| 49 | + |
| 50 | +## ... code abbreviated here to get to the appropriate examples ... |
| 51 | + |
| 52 | + |
| 53 | +@attr.s(frozen=True, cmp=False, hash=False, repr=True) |
| 54 | +class UserSettings(MethodView): |
| 55 | + form = attr.ib(factory=settings_form_factory) |
| 56 | + settings_update_handler = attr.ib(factory=settings_update_handler) |
| 57 | + |
| 58 | + decorators = [login_required] |
| 59 | + |
| 60 | + def get(self): |
| 61 | + return self.render() |
| 62 | + |
| 63 | + def post(self): |
| 64 | + if self.form.validate_on_submit(): |
| 65 | + try: |
| 66 | + self.settings_update_handler.apply_changeset( |
| 67 | + current_user, self.form.as_change() |
| 68 | + ) |
| 69 | + except StopValidation as e: |
| 70 | + self.form.populate_errors(e.reasons) |
| 71 | + return self.render() |
| 72 | + except PersistenceError: |
| 73 | + logger.exception("Error while updating user settings") |
| 74 | + flash(_("Error while updating user settings"), "danger") |
| 75 | + return self.redirect() |
| 76 | + |
| 77 | + flash(_("Settings updated."), "success") |
| 78 | + return self.redirect() |
| 79 | + return self.render() |
| 80 | + |
| 81 | + def render(self): |
| 82 | + return render_template("user/general_settings.html", form=self.form) |
| 83 | + |
| 84 | + def redirect(self): |
| 85 | +~~ return redirect(url_for("user.settings")) |
| 86 | + |
| 87 | + |
| 88 | +@attr.s(frozen=True, hash=False, cmp=False, repr=True) |
| 89 | +class ChangePassword(MethodView): |
| 90 | + form = attr.ib(factory=change_password_form_factory) |
| 91 | + password_update_handler = attr.ib(factory=password_update_handler) |
| 92 | + decorators = [login_required] |
| 93 | + |
| 94 | + def get(self): |
| 95 | + return self.render() |
| 96 | + |
| 97 | + def post(self): |
| 98 | + if self.form.validate_on_submit(): |
| 99 | + try: |
| 100 | + self.password_update_handler.apply_changeset( |
| 101 | + current_user, self.form.as_change() |
| 102 | + ) |
| 103 | + except StopValidation as e: |
| 104 | + self.form.populate_errors(e.reasons) |
| 105 | + return self.render() |
| 106 | + except PersistenceError: |
| 107 | + logger.exception("Error while changing password") |
| 108 | + flash(_("Error while changing password"), "danger") |
| 109 | + return self.redirect() |
| 110 | + |
| 111 | + flash(_("Password updated."), "success") |
| 112 | + return self.redirect() |
| 113 | + return self.render() |
| 114 | + |
| 115 | + def render(self): |
| 116 | + return render_template("user/change_password.html", form=self.form) |
| 117 | + |
| 118 | + def redirect(self): |
| 119 | +~~ return redirect(url_for("user.change_password")) |
| 120 | + |
| 121 | + |
| 122 | +@attr.s(frozen=True, cmp=False, hash=False, repr=True) |
| 123 | +class ChangeEmail(MethodView): |
| 124 | + form = attr.ib(factory=change_email_form_factory) |
| 125 | + update_email_handler = attr.ib(factory=email_update_handler) |
| 126 | + decorators = [login_required] |
| 127 | + |
| 128 | + def get(self): |
| 129 | + return self.render() |
| 130 | + |
| 131 | + def post(self): |
| 132 | + if self.form.validate_on_submit(): |
| 133 | + try: |
| 134 | + self.update_email_handler.apply_changeset( |
| 135 | + current_user, self.form.as_change() |
| 136 | + ) |
| 137 | + except StopValidation as e: |
| 138 | + self.form.populate_errors(e.reasons) |
| 139 | + return self.render() |
| 140 | + except PersistenceError: |
| 141 | + logger.exception("Error while updating email") |
| 142 | + flash(_("Error while updating email"), "danger") |
| 143 | + return self.redirect() |
| 144 | + |
| 145 | + flash(_("Email address updated."), "success") |
| 146 | + return self.redirect() |
| 147 | + return self.render() |
| 148 | + |
| 149 | + def render(self): |
| 150 | + return render_template("user/change_email.html", form=self.form) |
| 151 | + |
| 152 | + def redirect(self): |
| 153 | +~~ return redirect(url_for("user.change_email")) |
| 154 | + |
| 155 | + |
| 156 | +## ... code continues from here with similar redirect examples ... |
| 157 | +``` |
0 commit comments