URL Patterns with path()
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.
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.
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.
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. #}
- Forgetting the trailing slash in a path() string, causing inconsistent URLs across the site.
- Passing a view call, e.g. views.home(), instead of the function reference views.home, to path().
- Omitting the name= argument, which makes it harder to reference the URL later with {% url %} or reverse().
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: