0Pricing
Flask Academy · Lektion

Assets mit url_for static verknüpfen

Referenzieren Sie CSS und JS ohne fest codierte Pfade.

Assets mit url_for static verknüpfen ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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.

Stop Hardcoding Paths

Writing /static/style.css by hand in every template feels easy, but it is fragile. Flask gives you url_for to build asset links for you. 🔧

The static Endpoint

Flask registers a built-in endpoint named static for your assets. You ask url_for for it instead of typing the raw path yourself.

Pass the Filename

Call url_for with the endpoint and a filename argument. Flask returns the correct URL pointing into your static folder.

url_for("static", filename="style.css")
# -> /static/style.css

Use It in a Template

Inside a Jinja2 template you wrap the call in double braces. The link tag then receives a generated, always-correct href.

<link rel="stylesheet"
  href="{{ url_for('static', filename='style.css') }}">

Subfolders Work Too

Put the subpath right inside filename. Slashes in the value map straight onto folders within static.

url_for("static", filename="css/style.css")
# -> /static/css/style.css

Why Bother

If you ever change static_url_path, every url_for link updates itself. Hardcoded paths would all silently break instead.

Correct Prefixes for Free

When your app runs under a sub-path on a server, url_for adds the right prefix automatically, so links keep working everywhere.

Link Your JavaScript

The same call wires up scripts. Point the script tag at url_for and your JS loads from static without a hardcoded path.

<script src="{{ url_for('static', filename='js/app.js') }}"></script>

Images the Same Way

Pictures follow the identical pattern. Feed the filename to url_for and drop the result into the img tag source.

<img src="{{ url_for('static', filename='img/logo.png') }}">

Call It in Python Too

url_for is not template-only. Inside a view you can call url_for to compute an asset URL and return or log it.

from flask import url_for
link = url_for("static", filename="style.css")

One Habit to Keep

Make it a rule: every asset link goes through url_for. Future renames, prefixes, and cache busting then just work for you. ✅

Quick Check

You want a clean link to css/style.css inside static. Which call is right?

Recap: Linking Assets

You now use url_for with the static endpoint to build every asset link, so paths stay correct no matter how config changes. 🎯

Häufig gestellte Fragen

Ist die Lektion „Assets mit url_for static verknüpfen“ kostenlos?

Ja — der vollständige Text von „Assets mit url_for static verknüpfen“ 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 „Assets mit url_for static verknüpfen“?

Referenzieren Sie CSS und JS ohne fest codierte Pfade. 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 2 von 4.

Wie lange dauert die Lektion „Assets mit url_for static verknüpfen“?

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. Die Konvention für den static-Ordner
  2. Assets mit url_for static verknüpfen
  3. Cache Busting mit Dateiversionen
  4. Statische Dateien in der Produktionspraxis
← Zurück zu Flask Academy