Given the following urls.py setup, what URL pattern will match the path /blog/post/5/?
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'), ]
Remember how include() works with URL prefixes in Django.
The include('blog.urls') adds the blog/ prefix to all URLs defined in blog/urls.py. So post/5/ inside blog/urls.py becomes /blog/post/5/ in the project.
Choose the correct way to include the URLs from an app named shop inside the project's urls.py.
The include() function expects a string with the module path to the app's urls.py.
Option C correctly passes the string 'shop.urls' to include(). Other options either miss quotes or use wrong module references.
Given this urls.py snippet, why does Django raise an error when starting the server?
from django.urls import path, include urlpatterns = [ path('store/', include('store.urls')), path('store/', include('store.urls')), ]
Think about what happens if two URL patterns match the same path prefix.
Defining two path('store/', ...) entries causes Django to have conflicting routes for the same URL prefix, which leads to errors or unexpected behavior.
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?
Check how the include() function sets the app_name when given as a tuple.
When including URLs with include(('forum.urls', 'forum'), namespace='forum'), the app_name is set to 'forum'. This is accessible via request.resolver_match.app_name.
Consider this project urls.py snippet:
path('api', include('api.urls'))What is the effect on URL matching and user requests?
Think about how Django treats trailing slashes in path() definitions.
When the path is defined without a trailing slash, Django matches the exact string. So '/api' matches, but '/api/' does not unless explicitly handled.