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

Dynamic URL Parameters

Angle brackets in a URL, like <int:id>, are a blank space Django fills in with real numbers or words from the web address, like a fill-in-the-blank form.

Capturing a Value

Wrapping a URL segment in angle brackets, like <int:post_id>, tells Django to capture that part of the path and pass it as a keyword argument to the view function.

Example: Capturing a Value

Wrapping a URL segment in angle brackets, like <int:post_id>, tells Django to capture that part of the path and pass it as a keyword argument to the view function.

markup
urlpatterns = [
    path('posts/<int:post_id>/', views.post_detail, name='post_detail'),
]
{# 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. #}

The View Receives the Argument

The view function must declare a parameter with the same name used in the URL pattern. Django calls the view with that value already converted to the right Python type.

Example: The View Receives the Argument

The view function must declare a parameter with the same name used in the URL pattern. Django calls the view with that value already converted to the right Python type.

markup
from django.http import HttpResponse

def post_detail(request, post_id):
    return HttpResponse(f'Showing post #{post_id}')
{# 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 Path Converters

Django ships several built-in converters: str (default, no slashes), int (positive integers), slug (letters, numbers, hyphens, underscores), uuid, and path (matches slashes too).

Note: Use <slug:slug> for SEO-friendly URLs like /posts/my-first-post/.

Example: Common Path Converters

Django ships several built-in converters: str (default, no slashes), int (positive integers), slug (letters, numbers, hyphens, underscores), uuid, and path (matches slashes too).

markup
urlpatterns = [
    path('posts/<slug:slug>/', views.post_by_slug),
    path('files/<path:filepath>/', views.serve_file),
]
{# 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 to accept the captured parameter as an argument in the view function signature.
  2. Using the wrong path converter (e.g. <str:pk> for a numeric primary key instead of <int:pk>).
  3. Mismatching the parameter name in the URL pattern and the view function's argument name.
Chapter Summary
  • Angle-bracket segments like <int:pk> capture part of the URL and pass it to the view as an argument.
  • Path converters (str, int, slug, uuid, path) validate and convert the captured value automatically.
  • The name inside the brackets must exactly match the corresponding parameter name in the view function.

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.