Dynamic URL Parameters
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.
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.
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).
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. #}
- Forgetting to accept the captured parameter as an argument in the view function signature.
- Using the wrong path converter (e.g. <str:pk> for a numeric primary key instead of <int:pk>).
- Mismatching the parameter name in the URL pattern and the view function's argument name.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: