0
0
Djangoframework~20 mins

Named URLs for maintainability in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Named URLs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of reversing a named URL?
Given the following Django URL pattern and usage, what will be the output of print(url)?
Django
from django.urls import path, reverse

urlpatterns = [
    path('profile/<int:user_id>/', lambda request: None, name='user-profile'),
]

url = reverse('user-profile', kwargs={'user_id': 42})
print(url)
Auser-profile/42/
B/profile/user_id/42/
C/profile/42/
D/profile/<int:user_id>/
Attempts:
2 left
💡 Hint
Remember that reverse uses the name and kwargs to build the URL string.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a named URL with a slug parameter?
Which of the following Django URL patterns correctly defines a named URL that accepts a slug parameter named post_slug?
Apath('post/<slug:post_slug>/', views.post_detail, name='post-detail')
Bpath('post/<post_slug:slug>/', views.post_detail, name='post-detail')
Cpath('post/<slug>/', views.post_detail, name='post-detail')
Dpath('post/<str:post_slug>/', views.post_detail, name='post-detail')
Attempts:
2 left
💡 Hint
Check the syntax for path converters in Django URLs.
🔧 Debug
advanced
2:00remaining
Why does this reverse call raise NoReverseMatch?
Given the URL pattern and reverse call below, why does the reverse call raise a NoReverseMatch error?
Django
urlpatterns = [
    path('article/<int:id>/', views.article_detail, name='article-detail'),
]

url = reverse('article-detail', kwargs={'pk': 5})
ABecause the URL pattern name 'article-detail' is misspelled in reverse
BBecause the URL pattern expects 'id' but reverse is given 'pk' as the keyword argument
CBecause reverse cannot be used with integer parameters
DBecause the URL pattern is missing a trailing slash
Attempts:
2 left
💡 Hint
Check the parameter names in the URL pattern and reverse call.
state_output
advanced
2:00remaining
What is the rendered output of this template using named URLs?
Given the URL pattern and template snippet below, what will be the rendered HTML output?
Django
{% raw %}
# urls.py
urlpatterns = [
    path('dashboard/', views.dashboard, name='dashboard'),
]

# template.html
<a href="{% url 'dashboard' %}">Go to Dashboard</a>
{% endraw %}
A<a href="/dashboard/">Go to Dashboard</a>
B<a href="dashboard">Go to Dashboard</a>
C<a href="/dashboard">Go to Dashboard</a>
D<a href="/dashboard/index/">Go to Dashboard</a>
Attempts:
2 left
💡 Hint
Remember Django adds trailing slashes by default in URLs.
🧠 Conceptual
expert
2:00remaining
Why are named URLs important for maintainability in Django projects?
Which of the following best explains why using named URLs improves maintainability in Django projects?
ANamed URLs prevent users from accessing URLs without authentication
BNamed URLs automatically optimize database queries related to URL routing
CNamed URLs enforce strict type checking on URL parameters at runtime
DNamed URLs allow changing URL patterns without updating every place the URL is used, by referring to the name instead of the path string
Attempts:
2 left
💡 Hint
Think about how URLs are referenced in templates and views.