Flask: Routing, Templates and Forms

Harry · 14 Sep 2026 · 3 views
Advertisement
Advertisement

Why Flask?

Flask is a micro-framework - minimal and flexible. You add only what you need.

A Minimal App

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def home():
    return "Hello from Flask!"

if __name__ == "__main__":
    app.run(debug=True)

Routing with Variables

@app.route("/post/<int:post_id>")
def post(post_id):
    return f"Post #{post_id}"

Rendering Templates

from flask import render_template

@app.route("/hello/<name>")
def hello(name):
    return render_template("hello.html", name=name)

In templates/hello.html:

<h1>Hello, {{ name }}!</h1>

Handling Forms

@app.route("/contact", methods=["GET", "POST"])
def contact():
    if request.method == "POST":
        email = request.form["email"]
        return f"Thanks, {email}!"
    return render_template("contact.html")

Key Points

  • Decorators map URLs to view functions.
  • Jinja templates use {{ ... }} for expressions.
  • Flask is perfect for small sites and APIs; combine with SQLAlchemy for data.
Share this post:

Comments (0)

Please login or register to comment.