URL parameters with angle brackets let you capture parts of a web address as variables. This helps your website show different content based on the URL.
URL parameters with angle brackets in Django
path('route/<converter:name>/', view_function, name='route_name')
The angle brackets <> capture URL parts as variables.
The converter defines the type of variable (like int, str, slug).
user_id.path('user/<int:user_id>/', views.user_detail, name='user_detail')
post_slug.path('post/<slug:post_slug>/', views.post_detail, name='post_detail')
category_name.path('category/<str:category_name>/', views.category_view, name='category_view')
This example shows a URL pattern that captures a username from the URL and passes it to the greet_user view. The view then returns a greeting message using that username.
from django.urls import path from django.http import HttpResponse # View function that uses URL parameter def greet_user(request, username): return HttpResponse(f"Hello, {username}!") # URL patterns list urlpatterns = [ path('hello/<str:username>/', greet_user, name='greet_user'), ]
Use the correct converter to match the expected data type.
If you omit the converter, Django treats the parameter as a string by default.
Angle brackets must be used exactly as shown to capture parameters.
Angle brackets <> in Django URLs capture parts of the URL as variables.
Converters like int, str, and slug define the variable type.
This helps create dynamic, readable URLs that pass data to views.
Practice
<int:id> in a Django URL pattern do?Solution
Step 1: Understand angle brackets in Django URLs
Angle brackets capture parts of the URL as variables to pass to views.Step 2: Recognize the converter and variable name
The syntax<int:id>captures an integer named 'id'.Final Answer:
Captures a part of the URL as an integer variable named 'id'. -> Option DQuick Check:
URL parameter with <int:id> = integer variable capture [OK]
- Thinking it creates a static URL segment
- Confusing query parameters with URL parameters
- Ignoring the type converter like int or str
Solution
Step 1: Identify correct converter and variable order
The correct syntax is <converter:variable_name>, so <slug:post_slug> is correct.Step 2: Check the full path syntax
path('post/<slug:post_slug>/', views.post_detail)usespath('post/<slug:post_slug>/', views.post_detail), which is valid Django syntax.Final Answer:
path('post/<slug:post_slug>/', views.post_detail) -> Option BQuick Check:
Converter before variable name =path('post/<slug:post_slug>/', views.post_detail)[OK]
- Swapping converter and variable name
- Missing colon between converter and variable
- Using invalid syntax like <slug>post_slug
path('user/<int:user_id>/', views.profile), what will user_id be if the URL is /user/42/?Solution
Step 1: Understand the int converter effect
The <int:user_id> converter converts the URL segment to an integer.Step 2: Apply to the URL segment '42'
The string '42' from the URL is converted to the integer 42 before passing to the view.Final Answer:
The integer 42 -> Option CQuick Check:
int converter converts URL part to integer [OK]
- Assuming the parameter is a string
- Confusing slug with int converter
- Expecting a syntax error for valid syntax
path('article/<int:id>', views.detail)Solution
Step 1: Check angle bracket syntax
The pattern uses <int:id> without the closing '>' character, which is invalid syntax.Step 2: Confirm correct syntax
The correct syntax requires both opening and closing angle brackets: <int:id>.Final Answer:
Missing closing angle bracket '>' after 'id'. -> Option AQuick Check:
Angle brackets must be complete <int:id> [OK]
- Forgetting the closing '>'
- Confusing converter types
- Assuming view import errors cause syntax issues
Solution
Step 1: Choose correct converter for username
Usernames usually contain letters, numbers, underscores, so 'slug' converter fits best.Step 2: Confirm integer converter for post ID
Post ID should be an integer, so <int:post_id> is correct.Step 3: Evaluate options
path('profile/<slug:username>/post/<int:post_id>/', views.post_view)uses <slug:username> and <int:post_id>, matching requirements perfectly.Final Answer:
path('profile/<slug:username>/post/<int:post_id>/', views.post_view) -> Option AQuick Check:
Slug for username + int for post ID =path('profile/<slug:username>/post/<int:post_id>/', views.post_view)[OK]
- Using str converter which allows spaces
- Using int converter for username
- Omitting converters causing string default
Ready to prove it?
Pass the test and this pattern moves unknown
