0Pricing
Django Academy · Lektion

Das url-Tag und das static-Tag

Verknüpfen Sie Routen und Assets, ohne Pfade fest einzuprogrammieren.

Das url-Tag und das static-Tag ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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 Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.

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

Never Hardcode Paths

Typing URLs and file paths by hand breaks the moment they change. Django gives you the url and static tags to build them safely. 🔗

The url Tag Builds Routes

The url tag turns a route name into its real path. Rename the URL pattern later and every link updates automatically.

<a href="{% url 'home' %}">Home</a>

Name Your Routes First

For url to work, give each pattern a name in urls.py. That name is the stable handle the template refers to.

path("about/", views.about, name="about")

Passing URL Arguments

If a route needs a value, pass it after the name. Django slots the argument into the path where the converter expects it.

{% url 'post_detail' post.id %}

Namespaced URL Names

When an app sets an app_name, reference routes with a prefix like blog:detail to avoid clashing with other apps.

{% url 'blog:detail' post.id %}

The static Tag for Assets

The static tag builds the correct URL to a CSS, JS, or image file, even when storage paths differ in production.

{% static 'css/style.css' %}

Load static First

Before using the tag you must load static near the top of the template, which pulls the tag into that file.

{% load static %}

Linking a Stylesheet

Combine the pieces to link a stylesheet the safe way, so the path stays valid no matter where files are served from.

<link rel="stylesheet" href="{% static 'css/style.css' %}">

Showing an Image

The same static tag works for images. Point it at a file under your static folder and Django resolves the full URL.

<img src="{% static 'img/logo.png' %}">

Why static Matters

In production, assets often move to a CDN or hashed path. The static tag hides that, so your templates never need editing.

Two Tags, One Goal

Both tags share a purpose: let Django compute paths so you do not. Use url for views and static for files.

Quick Check

Pick the right tag for the right job.

Recap: Safe Links

You learned to build view paths with the url tag and asset paths with static, after loading static. That wraps up rendering HTML with data.

Häufig gestellte Fragen

Ist die Lektion „Das url-Tag und das static-Tag“ kostenlos?

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

Was lerne ich in „Das url-Tag und das static-Tag“?

Verknüpfen Sie Routen und Assets, ohne Pfade fest einzuprogrammieren. Du übst Django 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 Django Academy zu starten?

Keine Vorkenntnisse erforderlich. Django 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 4 von 4.

Wie lange dauert die Lektion „Das url-Tag und das static-Tag“?

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 Django Academy-Lektion Code schreiben und ausführen?

Ja. Jede Django 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. render() und der Template-Kontext
  2. Template-Tags und Filter
  3. Template-Vererbung mit extends und block
  4. Das url-Tag und das static-Tag
← Zurück zu Django Academy