-
Notifications
You must be signed in to change notification settings - Fork 0
Routing
Betrand edited this page Feb 26, 2026
·
1 revision
Basic Route
@app.route("/about")
def about(request):
return "About"@app.route() defaults to GET.
Multiple Methods
@app.route("/contact", methods=["GET", "POST"])
def contact(request):
if request.method == "POST":
return "Submitted"
return "Contact"Method Shortcuts
@app.get("/hello/<name>")
def hello(request, name):
return f"Hello {name}"
@app.get("/user/<int:user_id>")
def user(request, user_id):
return f"User {user_id}"
@app.get("/file/<path:filepath>")
def file_route(request, filepath):
return filepathURL Building
@app.get("/users/<int:user_id>", name="user_detail")
def user_detail(request, user_id):
return f"User {user_id}"
@app.get("/")
def index(request):
url = app.url_for("user_detail", user_id=42)
return f"Profile URL: {url}"Route Groups
api = app.group("/api")
@api.get("/users")
def users(request):
return {"ok": True}Group middleware:
def api_mw(request, call_next):
response = call_next(request)
response.headers["X-API"] = "1"
return response
api = app.group("/api", middleware=[api_mw])Per-Route Options
@app.post("/webhook", csrf=False)
def webhook(request):
return "ok"Supported options:
-
name="route_name"forapp.url_for -
middleware=[...]route-specific middleware -
csrf=Falsedisable CSRF on that route