Challenge - 5 Problems
URL Parameter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1: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), ]
Attempts:
2 left
💡 Hint
Remember that
<int:id> converts the URL part to an integer before passing it.✗ Incorrect
The
<int:id> converter in Django URL patterns parses the URL segment as an integer and passes it as an int to the view.📝 Syntax
intermediate1: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.Attempts:
2 left
💡 Hint
Django uses specific converters like
str, int, slug.✗ Incorrect
The correct syntax uses
<str:username> to capture a string parameter named username.🔧 Debug
advanced2: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),
]Attempts:
2 left
💡 Hint
Check what the
<int:product_id> converter expects.✗ Incorrect
The
<int:product_id> converter only matches digits. 'abc123' contains letters, so the URL pattern does not match and Django returns 404.❓ state_output
advanced1: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?Attempts:
2 left
💡 Hint
The slug converter matches letters, numbers, underscores, and hyphens.
✗ Incorrect
The slug converter captures the URL part as a string containing letters, numbers, underscores, or hyphens. The value is 'hello-world-2024'.
🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
Angle brackets without a colon (<param>) default to the
str converter.✗ Incorrect
Django treats <page_num> (without a converter and colon) as using the default
str converter for the 'page_num' parameter.