0Pricing
Flask Academy · Lektion

URLs mit url_for erstellen

Generieren Sie Links aus Endpoint-Namen statt aus fest codierten Pfaden.

URLs mit url_for erstellen ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🧩

Häufig gestellte Fragen

Ist die Lektion „URLs mit url_for erstellen“ kostenlos?

Ja — der vollständige Text von „URLs mit url_for erstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „URLs mit url_for erstellen“?

Generieren Sie Links aus Endpoint-Namen statt aus fest codierten Pfaden. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Flask Academy zu starten?

Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „URLs mit url_for erstellen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?

Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Variablen aus dem Pfad erfassen
  2. Typkonverter: int, float, string, path
  3. URLs mit url_for erstellen
  4. Abschließende Schrägstriche und Weiterleitungen
← Zurück zu Flask Academy