0
0
Djangoframework~20 mins

urlpatterns list structure in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django URL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this urlpatterns list do?
Given this Django urlpatterns list, what URL path will trigger the home_view function?
Django
from django.urls import path
from .views import home_view

urlpatterns = [
    path('', home_view, name='home'),
    path('about/', lambda request: None, name='about'),
]
AThe root URL path '/' triggers home_view
BNo URL triggers home_view
CThe URL path '/home/' triggers home_view
DThe URL path '/about/' triggers home_view
Attempts:
2 left
💡 Hint
Look at the first path's first argument, it defines the URL pattern.
📝 Syntax
intermediate
2:00remaining
Which urlpatterns list is syntactically correct?
Select the urlpatterns list that will NOT cause a syntax error in Django.
Aurlpatterns = {path('home/', home_view), path('about/', about_view)}
Burlpatterns = [path('home/', home_view), path('about/', about_view)]
Curlpatterns = (path('home/', home_view), path('about/', about_view))
Durlpatterns = path('home/', home_view), path('about/', about_view)
Attempts:
2 left
💡 Hint
urlpatterns must be a list, not a set or tuple.
state_output
advanced
2:00remaining
How many URL patterns are in this urlpatterns list?
Count the number of URL patterns defined in this urlpatterns list.
Django
from django.urls import path, include

urlpatterns = [
    path('blog/', include('blog.urls')),
    path('shop/', include('shop.urls')),
    path('contact/', lambda request: None),
]
A1
B2
C0
D3
Attempts:
2 left
💡 Hint
Each path() call counts as one URL pattern.
🔧 Debug
advanced
2:00remaining
What error does this urlpatterns list cause?
Identify the error caused by this urlpatterns list.
Django
from django.urls import path

urlpatterns = [
    path('home/', home_view),
    path('about/', about_view),
    path('contact/', contact_view)
]
ATypeError because path() arguments are wrong
BNo error, code runs fine
CNameError because home_view is not defined
DSyntaxError due to missing closing parenthesis
Attempts:
2 left
💡 Hint
Check the parentheses in the last path() call.
🧠 Conceptual
expert
3:00remaining
What is the effect of including another URLconf in urlpatterns?
In Django, what happens when you use path('blog/', include('blog.urls')) in urlpatterns?
AIt adds all URL patterns from blog.urls under the 'blog/' path prefix
BIt replaces the current urlpatterns with blog.urls
CIt causes a runtime error because include() cannot be used in urlpatterns
DIt creates a redirect from 'blog/' to blog.urls
Attempts:
2 left
💡 Hint
Think about how include() helps organize URLs.