Las etiquetas url y static
Enlace rutas y recursos sin codificar rutas manualmente
Las etiquetas url y static es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 4 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.
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.
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 «Las etiquetas url y static» es gratis?
Sí — el texto completo de «Las etiquetas url y 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 «Las etiquetas url y static»?
Enlace rutas y recursos sin codificar rutas manualmente 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 4 de 4.
¿Cuánto tiempo toma la lección «Las etiquetas url y 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
- render() y el contexto de la plantilla
- Etiquetas y filtros de plantilla
- Herencia de plantillas con extends y block
- Las etiquetas url y static