0
0
Djangoframework~20 mins

path function for routes in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Path 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 following URL pattern and a request to '/blog/42/', what view will be called and what will be the value of the captured parameter?
Django
from django.urls import path
from . import views

urlpatterns = [
    path('blog/<int:post_id>/', views.post_detail, name='post_detail'),
]

# Assume views.post_detail is defined to accept post_id as argument.
AThe view 'post_detail' is called with post_id='blog/42/'
BThe view 'post_detail' is called with post_id='42/'
CNo view is called because the URL does not match
DThe view 'post_detail' is called with post_id=42
Attempts:
2 left
💡 Hint
Look at how the path converter works in the URL pattern.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a path with a string parameter named 'username'?
Select the correct Django path function usage to capture a string parameter 'username' from the URL '/user//'
Apath('user/<str:username>/', views.profile, name='profile')
Bpath('user/<username>/', views.profile, name='profile')
Cpath('user/<int:username>/', views.profile, name='profile')
Dpath('user/<slug:username>/', views.profile, name='profile')
Attempts:
2 left
💡 Hint
Remember the syntax for path converters in Django's path function.
🔧 Debug
advanced
2:00remaining
Why does this URL pattern cause a runtime error?
Examine the following urlpatterns list and identify why it causes an error when Django starts.
Django
from django.urls import path

urlpatterns = [
    path('article/<int:id>/', views.article_detail),
    path('article/<int:id>/', views.article_edit),
]
AMissing 'name' argument in path causes runtime error.
BThe views module is not imported, causing NameError.
CDuplicate URL patterns with the same path cause ambiguity and Django raises an error.
DThe variable name 'id' is reserved and causes error.
Attempts:
2 left
💡 Hint
Check if all referenced modules are imported.
state_output
advanced
2:00remaining
What is the value of 'kwargs' passed to the view for this path?
Given the URL pattern and a request to '/shop/electronics/123/', what is the kwargs dictionary passed to the view?
Django
path('shop/<slug:category>/<int:item_id>/', views.item_detail, name='item_detail')
A{'category': 'electronics', 'item_id': 123}
B{'category': 'electronics/123', 'item_id': None}
C{'category': 'shop', 'item_id': 'electronics'}
D{} (empty dictionary)
Attempts:
2 left
💡 Hint
Look at how multiple path converters capture parts of the URL.
🧠 Conceptual
expert
3:00remaining
Which option best explains the difference between path() and re_path() in Django routing?
Choose the most accurate explanation about the difference between path() and re_path() functions in Django URL configuration.
Apath() automatically escapes special characters; re_path() does not.
Bpath() supports only static URLs; re_path() supports dynamic URLs with parameters.
Cpath() uses simpler, readable converters for common patterns; re_path() uses full regular expressions for complex matching.
Dpath() is deprecated and replaced by re_path() in modern Django versions.
Attempts:
2 left
💡 Hint
Think about the syntax and flexibility differences between path and re_path.