The {% url %} Tag in Templates
In this page:
Why Use {% url %} Instead of Hardcoding
{% url %} looks up a URL pattern by its registered name and builds the actual path from it, so links automatically stay correct if the underlying pattern changes.
Example: Why Use {% url %} Instead of Hardcoding
This renders as <a href="/about/">, generated from the URL pattern named about rather than a hardcoded string.
<!-- urls.py has: path('about/', views.about, name='about') -->
<a href="{% url 'about' %}">About Us</a>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Passing Arguments to {% url %}
When a URL pattern has dynamic segments (like an ID), pass the matching values directly after the URL name in {% url %}.
Warning: Passing the wrong number of arguments raises a NoReverseMatch error at render time.
Example: Passing Arguments to {% url %}
article.id is passed as the URL argument, producing a link like /articles/7/ for an article whose id is 7.
<!-- urls.py has: path('articles/<int:article_id>/', views.detail, name='article-detail') -->
<a href="{% url 'article-detail' article.id %}">{{ article.title }}</a>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Naming URL Patterns for Reuse
Every path() intended for use with {% url %} must declare a unique name, which is what the tag actually looks up.
Example: Naming URL Patterns for Reuse
These two names, home and about, are exactly what {% url home %} and {% url about %} in templates will resolve.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Hardcoding links as <a href="/blog/5/"> instead of {% url %}, which breaks silently if the URL pattern ever changes.
- Forgetting to pass required URL parameters to {% url %}, causing a NoReverseMatch error at render time.
- Using a URL name that isn't actually registered in urls.py (typo or missing name='...' argument on the path()).
- {% url name %} generates the correct URL for a named URL pattern instead of hardcoding paths.
- Every path() in urls.py needs a name='...' argument for {% url %} to reference it.
- Arguments for dynamic URL segments are passed directly after the URL name.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: