Bootstrap FreeKB - Flask - Redirect to a Route using redirect and url_for
Flask - Redirect to a Route using redirect and url_for

Updated:   |  Flask articles

Flask uses the MVC (Model View Controller) Framework. Just to make this as obvious as possible, I like my Flask apps to have the following.

  • Model -> models.py
  • View -> views.py
  • Controller -> __init__.py

For example, let say you have the following.

├── main.py
├── my-project (directory)
│   ├── __init__.py
│   ├── views.py
│   ├── templates (directory)
│   │   ├── base.html
│   │   ├── home.html
│   │   ├── foo.html
│   │   ├── bar.html

 

redirect can be used to redirect a user.

Here is an example of how you could use redirect. In this example, when requesting the /foo endpoint there will be an immediate redirect to the /bar endpoint.

from flask import Blueprint, render_template, redirect, url_for

views = Blueprint('views', __name__)

@views.route('/foo')
def home():
    return redirect('/bar')

@views.route('/bar')
def bar():
    return render_template('bar.html')

 

Or, you could redirect to some other URL.

from flask import Blueprint, render_template, redirect, url_for

views = Blueprint('views', __name__)

@views.route('/foo')
def home():
    return redirect('http://www.example.com')

 

Often, url_for is also used. Here is an example of how you could use url_for in a Blueprint Route to redirect a user from /foo.html to /bar.html.

In this example "views" in the name of the Blueprint you want to redirect to and "bar" is the name of the route.

from flask import Blueprint, render_template, redirect, url_for

views = Blueprint('views', __name__)

@views.route('/foo')
def home():
    return redirect(url_for('views.bar'))

@views.route('/bar')
def bar():
    return render_template('bar.html')

 

Let's say you want to redirect a user to an HTML page with parameters. For example, to redirect a user to foo.html?token=xyz.

from flask import Blueprint, render_template

views = Blueprint('views', __name__)

@views.route('/foo')
def foo():
    return render_template('foo.html?token=xyz')

 

If you try to do this with only render_template, a TemplateNotFound exception will be raised, something like this.

jinja2.exceptions.TemplateNotFound: foo.html?token=xyz

 

Here is how you can redirect a user to an HTML page with parameters using url_for and render_template. In this example, when the user requests the /foo endpoint they will be redirected to /bar?greeting='hello'

from flask import Blueprint, render_template, redirect, url_for

views = Blueprint('views', __name__)

@views.route('/foo')
def home():
    return redirect(url_for('views.bar', greeting='hello'))

@views.route('/bar')
def bar():
    return render_template('bar.html')
    print(f"greeting = {request.args.get('greeting')}")

 

By default, redirect will be a GET request. code=307 can be used to redirect as a POST request.

from flask import Blueprint, render_template, redirect, url_for

views = Blueprint('views', __name__)

@views.route('/foo')
def home():
    return redirect(url_for('views.bar'), code=307)

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 30af7a in the box below so that we can be sure you are a human.