← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 5 of 10

URL Patterns with path()

path() is like writing an address label — it tells Django exactly which web address should lead to which page.

Anatomy of path()

django.urls.path() takes a route string (without a leading slash), a view function, and an optional name keyword used to refer back to this URL elsewhere in the project.

Example: Anatomy of path()

django.urls.path() takes a route string (without a leading slash), a view function, and an optional name keyword used to refer back to this URL elsewhere in the project.

markup
from django.urls import path
from . import views

urlpatterns = [
    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. #}

Multiple Static Routes

A urlpatterns list usually holds several path() entries, one per static route in the app. Each maps a distinct URL string to its own view function.

Example: Multiple Static Routes

A urlpatterns list usually holds several path() entries, one per static route in the app. Each maps a distinct URL string to its own view function.

markup
urlpatterns = [
    path('', views.home, name='home'),
    path('contact/', views.contact, name='contact'),
    path('pricing/', views.pricing, name='pricing'),
]
{# 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 URLs for Reverse Lookups

The name= argument given to path() lets you refer to that URL elsewhere without hardcoding the string, using Django's {% url %} template tag or the reverse() function. If the URL path ever changes, only the path() call needs updating.

Note: Always name your URLs, even ones you think you will never need to look up.

Example: Naming URLs for Reverse Lookups

The name= argument given to path() lets you refer to that URL elsewhere without hardcoding the string, using Django's {% url %} template tag or the reverse() function. If the URL path ever changes, only the path() call needs updating.

markup
from django.urls import reverse

# Anywhere in Python code:
url = reverse('about')
# In a template: <a href="{% url 'about' %}">About</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. #}
Common Mistakes
  1. Forgetting the trailing slash in a path() string, causing inconsistent URLs across the site.
  2. Passing a view call, e.g. views.home(), instead of the function reference views.home, to path().
  3. Omitting the name= argument, which makes it harder to reference the URL later with {% url %} or reverse().
Chapter Summary
  • path() takes a URL pattern string, a view function, and an optional name for reverse lookups.
  • Always pass the view as a reference (views.home), never call it (views.home()).
  • Naming every URL with name= makes templates and redirects resilient to URL changes.

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.