0Pricing
Flask Academy · Lesson

Build URLs with url_for

Generate links from endpoint names, not hardcoded paths.

Hardcoded URLs Are Fragile

Typing "/user/alice" by hand breaks the day you rename a route. Flask offers url_for to build links from your view names instead.

Build by Endpoint Name

url_for takes the view function name and returns its path. Change the route string later and every generated link updates automatically. 🔗

from flask import url_for
url_for('home')

All lessons in this course

  1. Capture Variables from the Path
  2. Type Converters: int, float, string, path
  3. Build URLs with url_for
  4. Trailing Slashes and Redirect Behavior
← Back to Flask Academy