STATICFILES und der static-Tag
Assets organisieren und referenzieren
STATICFILES und der static-Tag ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.
What Static Files Are
Your CSS, JavaScript, and design images do not change per request. Django calls these static files and serves them separately from your Python views. 🎨
The static App
Django ships django.contrib.staticfiles in INSTALLED_APPS by default. It finds, collects, and serves your assets so you do not wire that up by hand.
INSTALLED_APPS = [
'django.contrib.staticfiles',
]Where Files Live
By convention you keep an app's assets inside a folder named static right next to its views and templates. Django scans every app for that folder.
blog/
static/
blog/
style.cssNamespace Your Folder
Nest assets under a folder matching the app name, like static/blog/. This namespacing stops two apps from clobbering each other's style.css.
The STATIC_URL Setting
The STATIC_URL setting is the public path prefix browsers use to fetch assets. Requests starting with this prefix are routed to your static files.
STATIC_URL = '/static/'Load the static Tag
To reference assets in a template, first load the tag library with load static at the very top of the file.
{% load static %}Use the static Tag
The static tag turns a relative asset path into the correct full URL, so you never hardcode STATIC_URL into your HTML.
<link rel="stylesheet" href="{% static 'blog/style.css' %}">Why Not Hardcode
If you wrote /static/blog/style.css by hand, changing STATIC_URL later would break every page. The static tag keeps your links flexible. ✅
Linking JavaScript
The same static tag works for any asset type. Point a script tag at a JS file and Django resolves the path for you.
<script src="{% static 'blog/app.js' %}"></script>STATICFILES_DIRS
For project-wide assets that belong to no single app, add their folders to STATICFILES_DIRS and Django will search there too.
STATICFILES_DIRS = [BASE_DIR / 'assets']Finders Do the Work
Behind the scenes, static finders walk every app's static folder plus STATICFILES_DIRS to locate the file a template asks for.
Quick Check
Let us check how you reference a static file in a template.
Recap
You learned that static files are your CSS and JS, that they live in namespaced static folders, and that the static tag builds their URLs cleanly. 🎉
Häufig gestellte Fragen
Ist die Lektion „STATICFILES und der static-Tag“ kostenlos?
Ja — der vollständige Text von „STATICFILES und der 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 „STATICFILES und der static-Tag“?
Assets organisieren und referenzieren 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 1 von 4.
Wie lange dauert die Lektion „STATICFILES und der 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
- STATICFILES und der static-Tag
- collectstatic für Production
- MEDIA_ROOT und Datei-Uploads
- ImageField und Uploads bereitstellen