Flask - Redirect a URL

by
Jeremy Canfield |
Updated: May 20 2025
| Flask articles
There are a few different ways to redirect a user in Flask.
- Using render_template
- Using redirect (this article)
- Using redirect and url_for
- Using jquery load
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 to a URL.
- Redirect to a URL (such as http://www.example.com) (this article)
- Redirect to an HTML page using render_template
- Redirect to a Route using url_for
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 a fully qualified 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')
Let's say you want to redirect 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 redirect('foo.html?token=xyz')
Did you find this article helpful?
If so, consider buying me a coffee over at