0Pricing
Flask Academy · Lesson

Build URLs with url_for

Generate links from endpoint names, not hardcoded paths.

Build URLs with url_for is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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')

The Endpoint Is the Function Name

By default the endpoint is the view function's name, not its URL path. So def home() is referenced as url_for('home').

@app.route('/')
def home():
    return 'Hi'

Pass Dynamic Values

For routes with captured parts, supply them as keyword arguments. url_for drops each value into the matching angle-bracket slot.

url_for('profile', name='alice')
# -> '/user/alice'

Extra Args Become Query Strings

Arguments that do not fit a route slot are added as a query string. So passing page=2 appends ?page=2 to the generated URL.

url_for('search', q='cats', page=2)
# -> '/search?q=cats&page=2'

Use It Inside Templates

Templates call url_for too, so your HTML links never go stale. It is the standard way to write hrefs in Jinja2 pages.

<a href="{{ url_for('home') }}">Home</a>

Refactor Without Fear

Because links derive from endpoint names, you can rename a URL path freely. Every url_for call keeps working, so refactors stay safe.

Redirect Pairs Well with It

Combine url_for with redirect to send users to a named view. You get a clean, refactor-proof redirect without writing the path by hand.

from flask import redirect
redirect(url_for('home'))

Absolute URLs When Needed

Pass _external=True to get a full http://host/path link. This is handy for emails or anywhere a relative path will not do.

url_for('home', _external=True)

It Knows the static Folder

url_for also builds links to assets via the static endpoint. That keeps CSS and image paths correct even if your app mounts elsewhere.

url_for('static', filename='app.css')

A Habit Worth Forming

Make url_for your default for every internal link. Hardcoded strings creep in and rot; generated URLs stay correct as your app evolves.

Quick Check

You call url_for('profile', name='alice', page=2). Where does page=2 end up?

Recap: You Built URLs Smartly

url_for turns endpoint names into paths, fills route variables, and tacks extras onto the query string. Next: trailing slashes. 🧩

Frequently asked questions

Is the “Build URLs with url_for” lesson free?

Yes — the full text of “Build URLs with url_for” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Build URLs with url_for”?

Generate links from endpoint names, not hardcoded paths. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Build URLs with url_for” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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