0
0
Djangoframework~20 mins

URL parameters with angle brackets in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Parameter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What is the output of this Django URL pattern match?
Given the URL pattern path('item/<int:id>/', view_function) and a request to /item/42/, what value does the view receive for id?
Django
from django.urls import path

def view_function(request, id):
    return id

urlpatterns = [
    path('item/<int:id>/', view_function),
]
AThe view receives the string '42'
BThe view receives the integer 42
CThe view receives None
DThe view raises a ValueError
Attempts:
2 left
💡 Hint
Remember that <int:id> converts the URL part to an integer before passing it.
📝 Syntax
intermediate
1:30remaining
Which URL pattern syntax is correct to capture a string parameter named 'username'?
Choose the correct Django URL pattern syntax to capture a string parameter called username.
Apath('user/<str:username>/', user_view)
Bpath('user/username/', user_view)
Cpath('user/<string:username>/', user_view)
Dpath('user/<text:username>/', user_view)
Attempts:
2 left
💡 Hint
Django uses specific converters like str, int, slug.
🔧 Debug
advanced
2:00remaining
Why does this URL pattern cause a 404 error for /product/abc123/?
Given the URL pattern path('product/<int:product_id>/', product_view), why does accessing /product/abc123/ cause a 404 error?
Django
urlpatterns = [
    path('product/<int:product_id>/', product_view),
]
ABecause 'abc123' is not an integer, so the pattern does not match
BBecause the URL pattern should use <str:product_id> instead
CBecause the URL is missing a trailing slash
DBecause the view function is missing
Attempts:
2 left
💡 Hint
Check what the <int:product_id> converter expects.
state_output
advanced
1:30remaining
What is the value of 'slug' captured by this URL pattern?
Given the URL pattern path('blog/<slug:slug>/', blog_view) and a request to /blog/hello-world-2024/, what is the value of slug passed to the view?
A'hello/world/2024'
B'hello world 2024'
C'hello_world_2024'
D'hello-world-2024'
Attempts:
2 left
💡 Hint
The slug converter matches letters, numbers, underscores, and hyphens.
🧠 Conceptual
expert
2:30remaining
What happens when using angle brackets without specifying a converter in Django URL patterns?
Consider the URL pattern path('page/<page_num>/', page_view). What behavior occurs?
ADjango raises a ValueError about invalid path converter syntax
BDjango raises a TypeError in the view function
CDjango treats 'page_num' as a string parameter by default
DDjango ignores the angle brackets and matches literally
Attempts:
2 left
💡 Hint
Angle brackets without a colon (<param>) default to the str converter.