0
0
Djangoframework~20 mins

Reverse URL resolution with reverse in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reverse URL 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 reverse() with named URL and no args?
Given the following URL pattern and code, what will reverse('home') return?
Django
from django.urls import path, reverse
from django.http import HttpResponse

urlpatterns = [
    path('', lambda request: HttpResponse('Home'), name='home'),
]

url = reverse('home')
A"/"
B"/home/"
C""
D"/index/"
Attempts:
2 left
💡 Hint
Think about the URL pattern for the empty string path.
📝 Syntax
intermediate
2:00remaining
Which reverse() call correctly resolves URL with one argument?
Given the URL pattern path('article//', view, name='article-detail'), which reverse call correctly builds the URL for id=5?
Django
from django.urls import reverse

# URL pattern: path('article/<int:id>/', view, name='article-detail')
Areverse('article-detail', args={'id': 5})
Breverse('article-detail', kwargs={'pk': 5})
Creverse('article-detail', args=[5])
Dreverse('article-detail', kwargs=[5])
Attempts:
2 left
💡 Hint
Check the parameter name and how args and kwargs are used.
🔧 Debug
advanced
2:00remaining
What error does this reverse() call raise?
Given the URL pattern path('profile//', view, name='profile'), what error occurs when calling reverse('profile') without arguments?
Django
from django.urls import reverse

# URL pattern: path('profile/<str:username>/', view, name='profile')
url = reverse('profile')
ANoReverseMatch
BTypeError
CValueError
DKeyError
Attempts:
2 left
💡 Hint
The URL requires a parameter but none was given.
state_output
advanced
2:00remaining
What is the value of url after reverse with kwargs?
Given the URL pattern path('blog//', view, name='blog-detail'), what is the value of url after this code?

url = reverse('blog-detail', kwargs={'slug': 'my-first-post'})
Django
from django.urls import reverse

# URL pattern: path('blog/<slug:slug>/', view, name='blog-detail')
url = reverse('blog-detail', kwargs={'slug': 'my-first-post'})
A"/blog/my-first-post"
B"/blog/my-first-post/"
C"/blog//"
D"/blog/my_first_post/"
Attempts:
2 left
💡 Hint
Check the trailing slash in the URL pattern.
🧠 Conceptual
expert
2:00remaining
Why use reverse() instead of hardcoding URLs in Django templates?
Which is the best reason to use reverse() or the {% url %} template tag instead of hardcoding URLs in Django projects?
AIt automatically translates URLs to different languages.
BIt makes the code run faster by caching URLs at compile time.
CIt allows URLs to be encrypted for security.
DIt ensures URLs update automatically if URL patterns change, avoiding broken links.
Attempts:
2 left
💡 Hint
Think about maintainability and avoiding mistakes.