Flask - Passing values into a route
by
Jeremy Canfield |
Updated: February 25 2024
| Flask articles
If you are not familair with Routes, check out my article creating endpoints using Routes.
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
Let's say your Flask app has the following files.
├── main.py
├── database (directory)
│ ├── example.db
├── my-project (directory)
│ ├── __init__.py
│ ├── views.py
│ ├── models.py
│ ├── templates (directory)
│ │ ├── base.html
│ │ ├── home.html
│ │ ├── results.html
│ └── static (directory)
│ └── custom.css
For example, let's say your view (views.py in this example) has the following. In this example, "value" can be passed into the /Test page.
from flask import Blueprint, render_template
views = Blueprint('views', __name__)
@app.route("/Test/<value>")
def test(value):
return render_template('home.html', value=value)
And perhaps test.html has something like this, using the value.
value = {{ value }}
Navigating to /Test and including a value should pass the value into the test.html page.
Did you find this article helpful?
If so, consider buying me a coffee over at