Given the following Django URL configuration, what is the full URL path for the named URL blog:post_detail with post_id=5?
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')), ]
Remember that the app_name defines the namespace and the include path prefix is added before the app URLs.
The app_name 'blog' sets the namespace. The include('blog.urls') is under the path 'blog/'. So the named URL blog:post_detail with post_id=5 resolves to '/blog/post/5/'.
Which of the following code snippets correctly sets up URL namespacing in a Django app?
Remember that app_name sets the namespace and name in path should be simple, without namespace prefix.
Option A correctly sets app_name and uses a simple name in the path. Option A incorrectly prefixes the name with namespace. Option A repeats the namespace in name. Option A misuses include inside urlpatterns.
Given this setup, why does reverse('store:product_detail', kwargs={'pk': 10}) raise a NoReverseMatch error?
# store/urls.py app_name = 'store' urlpatterns = [ path('product/<int:id>/', view, name='product_detail'), ] # project/urls.py urlpatterns = [ path('shop/', include('store.urls')), ]
Check how the URL pattern parameters and reverse arguments match.
The URL pattern defines <int:id>, so when using keyword arguments in reverse, the key must be 'id'. Using kwargs={'pk': 10} fails to match the parameter name, raising NoReverseMatch. Option C identifies this mismatch. Positional args=[10] would also work.
Given the URL configuration below, what will this Django template output?
{% url 'accounts:login' %}# accounts/urls.py app_name = 'accounts' urlpatterns = [ path('login/', view, name='login'), ] # project/urls.py urlpatterns = [ path('user/', include('accounts.urls')), ]
Remember the include path prefix is added before the app URLs.
The namespace 'accounts' corresponds to the included URLs under 'user/'. The named URL 'login' is 'login/'. So the full URL is '/user/login/'.
Which of the following is the best explanation for why Django uses URL namespacing?
Think about how large projects with many apps avoid URL name clashes.
URL namespacing allows different apps to define the same URL names without clashing by grouping them under unique namespaces. This helps in reversing URLs unambiguously.