0
0
Djangoframework~20 mins

Including app URLs in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Inclusion 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 inclusion?

Given the following urls.py setup, what URL pattern will match the path /blog/post/5/?

Django
from django.urls import path, include

# project urls.py
urlpatterns = [
    path('blog/', include('blog.urls')),
]

# blog/urls.py
from django.urls import path

urlpatterns = [
    path('post/<int:id>/', lambda request, id: id, name='post-detail'),
]
A/blog/post/5/ does not match any URL pattern
B/post/5/ matches and calls the view with id=5
C/blog/post/5/ matches but id is passed as a string '5'
D/blog/post/5/ matches and calls the view with id=5
Attempts:
2 left
💡 Hint

Remember how include() works with URL prefixes in Django.

📝 Syntax
intermediate
1:30remaining
Which option correctly includes app URLs in Django?

Choose the correct way to include the URLs from an app named shop inside the project's urls.py.

Apath('shop/', include(shop.urls))
Bpath('shop/', include('shop.urls.py'))
Cpath('shop/', include('shop.urls'))
Dpath('shop/', include('shop'))
Attempts:
2 left
💡 Hint

The include() function expects a string with the module path to the app's urls.py.

🔧 Debug
advanced
2:00remaining
Why does this URL inclusion cause an error?

Given this urls.py snippet, why does Django raise an error when starting the server?

Django
from django.urls import path, include

urlpatterns = [
    path('store/', include('store.urls')),
    path('store/', include('store.urls')),
]
ADuplicate URL patterns with the same prefix cause a conflict error
BThe <code>include()</code> function cannot be called twice in the same file
CMissing trailing slash in the path causes the error
DDjango requires app URLs to be included only once per project
Attempts:
2 left
💡 Hint

Think about what happens if two URL patterns match the same path prefix.

state_output
advanced
2:00remaining
What is the value of request.resolver_match.app_name after including app URLs?

In the project urls.py, the app URLs are included as:

path('forum/', include(('forum.urls', 'forum'), namespace='forum'))

Inside a view called by /forum/thread/10/, what is the value of request.resolver_match.app_name?

Aforum.urls
Bforum
CNone
Dthread
Attempts:
2 left
💡 Hint

Check how the include() function sets the app_name when given as a tuple.

🧠 Conceptual
expert
2:30remaining
What happens if you include app URLs without a trailing slash in the path?

Consider this project urls.py snippet:

path('api', include('api.urls'))

What is the effect on URL matching and user requests?

AURLs must start with '/api' exactly, so '/api' matches but '/api/' does not
BDjango automatically adds a trailing slash, so both '/api' and '/api/' work
CRequests to '/api/' will redirect to '/api' automatically
DNo URLs under 'api' will match because the path is missing a trailing slash
Attempts:
2 left
💡 Hint

Think about how Django treats trailing slashes in path() definitions.