← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 10 of 10

The {% url %} Tag in Templates

The {% url %} tag looks up a page's address by its name, so links keep working even if the actual URL path changes later.

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.

markup
<!-- 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.

markup
<!-- 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.

markup
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. #}
Common Mistakes
  1. Hardcoding links as <a href="/blog/5/"> instead of {% url %}, which breaks silently if the URL pattern ever changes.
  2. Forgetting to pass required URL parameters to {% url %}, causing a NoReverseMatch error at render time.
  3. Using a URL name that isn't actually registered in urls.py (typo or missing name='...' argument on the path()).
Chapter Summary
  • {% 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.