0
0
Djangoframework~10 mins

URL parameters with angle brackets in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - URL parameters with angle brackets
Start: URL request
Django URL dispatcher reads pattern
Detect angle brackets <param>
Extract parameter value from URL
Pass parameter to view function
View uses parameter to generate response
Send response back to browser
Django reads the URL pattern with angle brackets, extracts the parameter from the URL, and passes it to the view function.
Execution Sample
Django
from django.urls import path
from django.http import HttpResponse

def view(request, name):
    return HttpResponse(f"Hello, {name}!")

urlpatterns = [
    path('hello/<str:name>/', view),
]
This code defines a URL pattern that captures a string parameter 'name' from the URL and passes it to the view.
Execution Table
StepURL RequestedURL Pattern MatchedParameter ExtractedView Called WithResponse Output
1/hello/Alice/hello/<str:name>/name = 'Alice'view(request, name='Alice')Hello, Alice!
2/hello/Bob/hello/<str:name>/name = 'Bob'view(request, name='Bob')Hello, Bob!
3/hello/123/hello/<str:name>/name = '123'view(request, name='123')Hello, 123!
4/hello/No matchNo parameterNo view called404 Not Found
💡 Execution stops when URL does not match the pattern or after response is sent.
Variable Tracker
VariableStartAfter 1After 2After 3After 4
nameNone'Alice''Bob''123'None
Key Moments - 3 Insights
Why does the URL '/hello/' not call the view function?
Because the URL pattern expects a parameter inside angle brackets, like '<str:name>', so '/hello/' without a parameter does not match the pattern (see execution_table row 4).
What happens if the parameter is a number but the pattern expects a string?
The pattern '<str:name>' accepts any string including numbers as strings, so '123' is accepted as a string parameter (see execution_table row 3).
How does Django know which part of the URL is the parameter?
Django uses the angle brackets in the URL pattern to identify the parameter position and type, then extracts that part from the URL (see concept_flow step 'Detect angle brackets <param>').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' when the URL '/hello/Bob/' is requested?
A'Alice'
B'Bob'
C'123'
DNone
💡 Hint
Check execution_table row 2 under 'Parameter Extracted' and 'View Called With'.
At which step does the URL not match the pattern and cause a 404 error?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look for 'No match' and '404 Not Found' in execution_table.
If the URL pattern was changed to 'hello/<int:name>/', what would happen when requesting '/hello/Alice/'?
AThe view is called with name='Alice'
BThe view is called with name=0
CThe URL does not match and returns 404
DThe server crashes
💡 Hint
Check how type converters like affect matching in the execution_table and concept_flow.
Concept Snapshot
Django URL patterns use angle brackets <param> to capture parts of the URL as parameters.
Syntax: path('route/<type:name>/', view).
The parameter is extracted from the URL and passed to the view.
If the URL does not match the pattern, Django returns 404.
Type converters like str, int specify expected parameter type.
Full Transcript
In Django, URL patterns can include parameters inside angle brackets, like <str:name>. When a URL request comes in, Django matches it against the pattern. If it matches, Django extracts the parameter value from the URL and passes it to the view function as an argument. For example, the URL '/hello/Alice/' matches the pattern 'hello/<str:name>/', so the parameter 'name' is set to 'Alice'. The view then uses this parameter to create a response, such as 'Hello, Alice!'. If the URL does not include the parameter or does not match the pattern, Django returns a 404 error. Type converters like str or int inside the angle brackets tell Django what type to expect and help with matching. This process allows dynamic URLs that can change based on user input or other data.