Bootstrap FreeKB - Flask - Creating endpoints using Routes
Flask - Creating endpoints using Routes

Updated:   |  Flask articles

A route is how Flask defines the endpoint pages for your app.

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
├── my-project (directory)
│   ├── __init__.py
│   ├── views.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, which creates two pages, the default / (Home) page and the /Test page.

from flask import Blueprint, render_template

@app.route("/")
def home():
    return render_template('home.html')

@app.route("/Test")
def test():
    return render_template('test.html')

 

Navigating to http://<hostname or IP address>/ should return home.html.

 

Navigating to http://<hostname or IP address>/Test should return test.html.

 

Whatever routes must use the same keyword that you set for Flask(__name__).

 

Or if using Blueprint, the same keyword that was set for Blueprint(__name__).

 




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 34e9fb in the box below so that we can be sure you are a human.