STATICFILES y la etiqueta static
Organice y referencie sus recursos
STATICFILES y la etiqueta static es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Django Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Django Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎉
Aprende Python con un tutor de IA — gratis
Escribe y ejecuta código real en tu navegador, obtén ayuda instantánea de un tutor de IA disponible 24/7 y continúa donde lo dejaste en la web o en la aplicación.
- Cursos
- 30
- Lecciones
- 120
Preguntas frecuentes
¿La lección «STATICFILES y la etiqueta static» es gratis?
Sí — el texto completo de «STATICFILES y la etiqueta static» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Django Academy, actualiza a CoddyKit PRO. El curso de Django Academy incluye 4 lecciones en total.
¿Qué aprenderé en «STATICFILES y la etiqueta static»?
Organice y referencie sus recursos Practicas Django Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Django Academy?
No se requiere experiencia previa. Django Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «STATICFILES y la etiqueta static»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Django Academy?
Sí. Cada lección de Django Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- STATICFILES y la etiqueta static
- collectstatic para producción
- MEDIA_ROOT y cargas de archivos
- ImageField y servicio de cargas