0
0
Djangoframework~20 mins

URL parameter type converters in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Parameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django URL pattern matching?
Given the URL pattern and view below, what will be the value of year inside the view when the URL /archive/2023/ is accessed?
Django
from django.urls import path
from django.http import HttpResponse

def archive_view(request, year):
    return HttpResponse(f"Year: {year}")

urlpatterns = [
    path('archive/<int:year>/', archive_view),
]
A"Year: 2023"
B"Year: '2023'" (string with quotes)
C"Year: 2023.0" (float value)
DRaises a ValueError because year is not converted
Attempts:
2 left
💡 Hint
The int converter converts the URL part to an integer type.
📝 Syntax
intermediate
1:30remaining
Which URL pattern correctly uses the slug converter?
Select the URL pattern that correctly captures a slug parameter named post_slug.
Apath('post/<slug_post>/', views.post_detail)
Bpath('post/<slug:post_slug>/', views.post_detail)
Cpath('post/<str:post_slug>/', views.post_detail)
Dpath('post/<slug>/', views.post_detail)
Attempts:
2 left
💡 Hint
The syntax is <converter:name>.
🔧 Debug
advanced
2:00remaining
Why does this URL pattern cause a 404 error for /user/abc123/?
Consider this URL pattern:
path('user//', views.user_profile)
Why does accessing /user/abc123/ return a 404 error?
ABecause the user_id parameter is not passed as a string
BBecause the view function is missing a required argument
CBecause the URL pattern is missing a trailing slash
DBecause 'abc123' is not a valid integer, so the pattern does not match
Attempts:
2 left
💡 Hint
The int converter only matches digits.
state_output
advanced
2:30remaining
What is the type and value of pk in this view?
Given this URL pattern and view:
path('item//', views.item_detail)
def item_detail(request, pk):
    return HttpResponse(type(pk).__name__ + ' ' + str(pk))
What will be the response content when accessing /item/123e4567-e89b-12d3-a456-426614174000/?
A"int 123e4567"
B"str 123e4567-e89b-12d3-a456-426614174000"
C"UUID 123e4567-e89b-12d3-a456-426614174000"
DRaises a ValueError because UUID is not converted
Attempts:
2 left
💡 Hint
The uuid converter converts the string to a UUID object.
🧠 Conceptual
expert
3:00remaining
Which statement about Django URL converters is true?
Select the correct statement about Django's built-in URL parameter type converters.
AThe <code>slug</code> converter matches only letters, numbers, underscores, and hyphens
BThe <code>path</code> converter matches any non-empty string including slashes
CThe <code>int</code> converter matches any integer including negative numbers
DThe <code>uuid</code> converter matches only lowercase hexadecimal digits without hyphens
Attempts:
2 left
💡 Hint
Think about what characters a slug can contain.