Flask Routing and Templates

Python 10 min min read Updated: Mar 09, 2026 Intermediate
Flask Routing and Templates
Intermediate Topic 2 of 10

Flask Routing & Templates

Flask routing and templates are essential components for building dynamic web applications. Routing determines how different URLs are handled by the application, while templates allow developers to generate dynamic HTML pages using Python data.

Flask uses the Jinja2 template engine to render HTML pages with dynamic content.

What is Routing in Flask?

Routing refers to mapping URLs to specific functions in a Flask application. Each route defines which function should run when a user visits a particular URL.

Flask uses the @app.route() decorator to define routes.

python from flask import Flask app = Flask(__name__) @app.route("/") def home(): return "Welcome to Flask Routing" if __name__ == "__main__": app.run(debug=True)

In this example, when the root URL (/) is accessed, the home() function runs and returns a message.

Multiple Routes

You can create multiple routes to handle different pages of a website.

python @app.route("/about") def about(): return "About Page" @app.route("/contact") def contact(): return "Contact Page"

Each route corresponds to a specific URL.

Dynamic Routes

Flask allows dynamic URL parameters that can pass values directly to functions.

python @app.route("/user/<name>") def user(name): return f"Hello {name}"

Visiting /user/Alice will display Hello Alice.

Route Parameters with Data Types

Flask supports specifying data types for route parameters.

python @app.route("/post/<int:id>") def show_post(id): return f"Post ID: {id}"

Here, the route only accepts integer values.

HTTP Methods in Routes

Flask routes can handle different HTTP methods such as GET and POST.

python @app.route("/login", methods=["GET", "POST"]) def login(): return "Login Page"

This allows the route to handle both GET and POST requests.

Introduction to Flask Templates

Templates allow developers to separate application logic from presentation. Flask uses the Jinja2 template engine to render dynamic HTML pages.

Templates are stored inside a folder named templates.

Rendering a Template

python from flask import render_template @app.route("/") def home(): return render_template("index.html")

This renders the HTML file located in the templates directory.

Example Template File

Create a file named templates/index.html.

html <html> <head> <title>Flask App</title> </head> <body> <h1>Welcome to Flask Templates</h1> </body> </html>

This HTML page will be rendered when the route is accessed.

Passing Data to Templates

Flask allows passing variables from Python code to HTML templates.

python @app.route("/") def home(): return render_template("index.html", name="Alice")

Inside the template:

html <h1>Hello {{ name }}</h1>

The variable name will be replaced with the value passed from Python.

Using Loops in Templates

Jinja2 templates support loops and conditions.

python @app.route("/users") def users(): user_list = ["Alice", "Bob", "Charlie"] return render_template("users.html", users=user_list)

Template file:

html <ul> {% for user in users %} <li>{{ user }}</li> {% endfor %} </ul>

This dynamically generates a list of users.

Using Conditions in Templates

html {% if users %} <p>Users available</p> {% else %} <p>No users found</p> {% endif %}

This allows conditional rendering inside HTML templates.

Project Structure

flask_app/
│
├── app.py
│
├── templates/
│   ├── index.html
│   └── users.html
│
└── static/
    ├── css/
    └── js/

This structure keeps application logic and frontend files organized.

Best Practices

  • Keep routing logic simple and clean.
  • Use templates to separate presentation from backend logic.
  • Use Jinja2 features for dynamic HTML generation.
  • Organize templates in a structured folder.

Conclusion

Flask routing allows developers to map URLs to specific functions, while templates enable dynamic HTML rendering using Python data. Together, they form the foundation for building dynamic web applications with Flask.

Understanding routing and templates is essential for developing scalable Flask applications and REST APIs.

In the next tutorial, we will explore Flask Forms & Request Handling and learn how Flask processes user input from web forms.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators