0
0
Djangoframework~20 mins

URL namespacing in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL Namespacing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What URL does this namespaced Django URL pattern resolve to?

Given the following Django URL configuration, what is the full URL path for the named URL blog:post_detail with post_id=5?

Django
from django.urls import path, include

# blog/urls.py
app_name = 'blog'
urlpatterns = [
    path('post/<int:post_id>/', lambda request: None, name='post_detail'),
]

# project/urls.py
urlpatterns = [
    path('blog/', include('blog.urls')),
]
A/blog/post_detail/5/
B/blog/post/5/
C/post_detail/5/
D/post/5/
Attempts:
2 left
💡 Hint

Remember that the app_name defines the namespace and the include path prefix is added before the app URLs.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a namespaced URL pattern in Django?

Which of the following code snippets correctly sets up URL namespacing in a Django app?

A
app_name = 'shop'
urlpatterns = [
    path('item/&lt;int:id&gt;/', view, name='item_detail'),
]
B
urlpatterns = [
    path('item/&lt;int:id&gt;/', view, name='shop:item_detail'),
]
C
app_name = 'shop'
urlpatterns = [
    path('item/&lt;int:id&gt;/', view, name='shop:item_detail'),
]
D
app_name = 'shop'
urlpatterns = [
    path('item/&lt;int:id&gt;/', view, name='item_detail'),
]
include('shop.urls', namespace='shop')
Attempts:
2 left
💡 Hint

Remember that app_name sets the namespace and name in path should be simple, without namespace prefix.

🔧 Debug
advanced
2:00remaining
Why does reversing 'store:product_detail' raise NoReverseMatch error?

Given this setup, why does reverse('store:product_detail', kwargs={'pk': 10}) raise a NoReverseMatch error?

Django
# store/urls.py
app_name = 'store'
urlpatterns = [
    path('product/<int:id>/', view, name='product_detail'),
]

# project/urls.py
urlpatterns = [
    path('shop/', include('store.urls')),
]
AThe namespace 'store' is not correctly set in the included URLs.
BThe reverse call uses args but the URL pattern requires kwargs with 'id'.
CThe URL pattern uses 'id' but reverse expects 'pk' as argument name.
DThe include path prefix 'shop/' does not match the namespace 'store'.
Attempts:
2 left
💡 Hint

Check how the URL pattern parameters and reverse arguments match.

state_output
advanced
2:00remaining
What is the output of this Django template URL tag with namespaces?

Given the URL configuration below, what will this Django template output?

{% url 'accounts:login' %}
Django
# accounts/urls.py
app_name = 'accounts'
urlpatterns = [
    path('login/', view, name='login'),
]

# project/urls.py
urlpatterns = [
    path('user/', include('accounts.urls')),
]
A/user/login/
B/accounts/login/
C/user/accounts/login/
D/login/
Attempts:
2 left
💡 Hint

Remember the include path prefix is added before the app URLs.

🧠 Conceptual
expert
2:00remaining
Why use URL namespacing in Django projects?

Which of the following is the best explanation for why Django uses URL namespacing?

ATo automatically generate URLs without defining them explicitly in each app's urls.py.
BTo speed up URL resolution by caching namespace lookups.
CTo enforce security by restricting URL access based on namespaces.
DTo allow multiple apps to use the same URL names without conflicts by grouping them under unique namespaces.
Attempts:
2 left
💡 Hint

Think about how large projects with many apps avoid URL name clashes.