What do angle brackets <> mean in Django URL patterns?
Angle brackets <> in Django URL patterns mark a variable part of the URL. They capture a value from the URL and pass it as a parameter to the view function.
Click to reveal answer
beginner
How do you capture an integer parameter named 'id' in a Django URL pattern?
Use <int:id> inside the URL pattern. This captures an integer from the URL and names it 'id' for the view to use.
Click to reveal answer
beginner
What happens if you use <str:name> in a Django URL pattern?
It captures any text (except slashes) from the URL and passes it as a string parameter named 'name' to the view.
Click to reveal answer
intermediate
Can you use custom converters with angle brackets in Django URLs?
Yes, Django allows custom path converters to define how URL parts are matched and converted before passing to views.
Click to reveal answer
intermediate
Why is it better to use typed parameters like <int:id> instead of just <id>?
Typed parameters ensure the URL part matches the expected type (like integer), preventing errors and improving URL clarity.
Click to reveal answer
What does <slug:post_slug> do in a Django URL pattern?
ACaptures a string with letters, numbers, underscores or hyphens as 'post_slug'
BCaptures only numbers as 'post_slug'
CCaptures any text including slashes as 'post_slug'
DCaptures a floating point number as 'post_slug'
✗ Incorrect
<slug:post_slug> captures a slug string which includes letters, numbers, underscores, or hyphens.
Which of these is the correct way to capture a username as a string in Django URL?
A/user/<str:username>/
B/user/<int:username>/
C/user/<slug:username>/
D/user/<float:username>/
✗ Incorrect
Use <str:username> to capture any string without slashes as the username.
What will happen if you use <int:id> but the URL has a non-integer value?
AThe server will crash
BThe view will receive the value as a string
CDjango will convert it to zero
DDjango will raise a 404 error (page not found)
✗ Incorrect
Django returns a 404 error if the URL part does not match the expected type.
How do you define a URL pattern that captures a UUID parameter named 'uid'?
A<int:uid>
B<uuid:uid>
C<str:uid>
D<slug:uid>
✗ Incorrect
Use <uuid:uid> to capture a UUID in Django URLs.
Which part of Django handles the matching of URL parameters with angle brackets?
AModels
BViews
CURL dispatcher (urls.py)
DTemplates
✗ Incorrect
The URL dispatcher in urls.py matches URL patterns and extracts parameters.
Explain how angle brackets are used to capture parameters in Django URL patterns.
Think about how URLs can include changing parts like IDs or names.
You got /4 concepts.
Describe why using typed parameters like is helpful in Django URLs.
Consider what happens if a URL expects a number but gets text.
You got /4 concepts.
Practice
(1/5)
1. What does the angle bracket syntax <int:id> in a Django URL pattern do?
easy
A. Creates a static URL segment named 'int:id'.
B. Specifies a query parameter instead of a URL segment.
C. Defines a string variable named 'id' without type conversion.
D. Captures a part of the URL as an integer variable named 'id'.
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 D
Quick Check:
URL parameter with <int:id> = integer variable capture [OK]
Hint: Angle brackets capture URL parts as typed variables [OK]
Common Mistakes:
Thinking it creates a static URL segment
Confusing query parameters with URL parameters
Ignoring the type converter like int or str
2. Which of the following is the correct syntax to capture a slug parameter named 'post_slug' in a Django URL pattern?
easy
A. path('post/<str:post_slug>/', views.post_detail)
B. path('post/<slug:post_slug>/', views.post_detail)
C. path('post/<post_slug:slug>/', views.post_detail)
D. path('post/<slug>post_slug/', views.post_detail)
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) uses path('post/<slug:post_slug>/', views.post_detail), which is valid Django syntax.
Final Answer:
path('post/<slug:post_slug>/', views.post_detail) -> Option B
Quick Check:
Converter before variable name = path('post/<slug:post_slug>/', views.post_detail) [OK]
Hint: Use <converter:variable> format for URL parameters [OK]
Common Mistakes:
Swapping converter and variable name
Missing colon between converter and variable
Using invalid syntax like <slug>post_slug
3. Given this URL pattern: path('user/<int:user_id>/', views.profile), what will user_id be if the URL is /user/42/?
medium
A. A slug '42'
B. The string '42'
C. The integer 42
D. A syntax error occurs
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 C
Quick Check:
int converter converts URL part to integer [OK]
Hint: int converter makes URL part an integer, not string [OK]
Common Mistakes:
Assuming the parameter is a string
Confusing slug with int converter
Expecting a syntax error for valid syntax
4. Identify the error in this Django URL pattern: path('article/<int:id>', views.detail)
medium
A. Missing closing angle bracket '>' after 'id'.
B. Using 'int' converter instead of 'str'.
C. View function 'detail' is not imported.
D. URL pattern should not use angle brackets.
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 A
Quick Check:
Angle brackets must be complete <int:id> [OK]
Hint: Always close angle brackets in URL parameters [OK]
Common Mistakes:
Forgetting the closing '>'
Confusing converter types
Assuming view import errors cause syntax issues
5. You want to create a URL pattern that captures a username (letters, numbers, underscores) and a post ID (integer). Which of these is the best Django URL pattern?
hard
A. path('profile/<slug:username>/post/<int:post_id>/', views.post_view)
B. path('profile/<str:username>/post/<int:post_id>/', views.post_view)
C. path('profile/<int:username>/post/<int:post_id>/', views.post_view)
D. path('profile/<username>/post/<post_id>/', views.post_view)
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 A
Quick Check:
Slug for username + int for post ID = path('profile/<slug:username>/post/<int:post_id>/', views.post_view) [OK]
Hint: Use slug for usernames, int for numeric IDs in URLs [OK]
Common Mistakes:
Using str converter which allows spaces
Using int converter for username
Omitting converters causing string default
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.