URL Dispatcher and Routing0%

URL Dispatcher and Routing

Beginner12 min read•Updated: Jul 11, 2026
Study MaterialsCheatsheet

URL Dispatcher and Routing

The URL Dispatcher is the front door of every Django application. It matches incoming HTTP request paths against a list of URL patterns, extracts dynamic parameters (like integers, slugs, or UUIDs) using Path Converters, and dispatches the request to the matching view callable.


1. Anatomy of path() and re_path()

Python
from django.urls import path, re_path
from . import views
 
urlpatterns = [
# 1. Static literal path
path("about/", views.about_view, name="about"),
 
# 2. Dynamic path with path converter
path("articles/<int:pk>/", views.article_by_id, name="by-id"),
 
# 3. Dynamic slug path
path("articles/<slug:slug>/", views.article_by_slug, name="by-slug"),
 
# 4. Regex path for specialized formats (e.g. 4-digit year)
re_path(r"^archive/(?P<year>[0-9]{4})/$", views.archive_view, name="archive"),
]

2. Built-in Path Converters

Django includes five native path converters that both match patterns and typecast extracted URL parameters into native Python types:

Path ConverterMatchesPython Type Passed to ViewExample
str (default)Any non-empty string excluding /str<str:username> matches "alice"
intZero or any positive integerint<int:pk> matches 42 -> passes 42
slugASCII letters, numbers, hyphens, underscoresstr<slug:post_slug> matches "django-orm-guide"
uuidFormatted UUID stringuuid.UUID<uuid:id> matches "123e4567-e89b-..."
pathAny non-empty string including /str<path:filepath> matches "media/docs/invoice.pdf"

3. Creating Custom Path Converters

If standard converters do not fit your domain (e.g. 4-digit years or phone numbers), register a custom converter:

Python
# config/converters.py
class FourDigitYearConverter:
regex = r"[0-9]{4}"
 
def to_python(self, value: str) -> int:
# Cast matched URL string into Python integer
return int(value)
 
def to_url(self, value: int) -> str:
# Cast Python value back to URL string for reverse()
return f"{value:04d}"

Register in urls.py:

Python
# config/urls.py
from django.urls import path, register_converter
from config.converters import FourDigitYearConverter
from . import views
 
# Register converter with tag name 'yyyy'
register_converter(FourDigitYearConverter, "yyyy")
 
urlpatterns = [
# Clean and readable!
path("archive/<yyyy:year>/", views.year_archive, name="year-archive"),
]

4. Namespacing and URL Resolution

Always define app_name inside your app's urls.py:

Python
# billing/urls.py
app_name = "billing"
 
urlpatterns = [
path("invoices/<int:pk>/", views.invoice_detail, name="invoice-detail"),
]

Resolve URLs unambiguously across your project:

  • In Python: reverse("billing:invoice-detail", kwargs={"pk": 105})
  • In Templates: {% url 'billing:invoice-detail' pk=invoice.id %}

Interactive Quiz

Practice Quiz

Question 1
What Python data type does the <int:pk> path converter pass into the view function?
Question 2
What characters are matched by the <slug:slug> path converter?
Question 3
What two methods must a custom path converter class implement?
Question 4
When should you use the <path:subpath> converter instead of <str:subpath>?
Question 5
What function registers a custom path converter with Django's URL dispatcher?
Next Lesson

Serializers

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Class-Based Views (CBV)Serializers

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext