Bootstrap FreeKB - Flask - Passing values into a route
Flask - Passing values into a route

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


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