Challenge - 5 Problems
Reverse URL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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')
Attempts:
2 left
💡 Hint
Think about the URL pattern for the empty string path.
✗ Incorrect
The URL pattern named 'home' matches the empty string path ''. So reverse('home') returns '/' which is the root URL.
📝 Syntax
intermediate2: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')
Attempts:
2 left
💡 Hint
Check the parameter name and how args and kwargs are used.
✗ Incorrect
The URL pattern uses id as the parameter name. Using args=[5] passes the positional argument correctly. Option C uses wrong key pk. Option C uses wrong type for args. Option C uses wrong type for kwargs.
🔧 Debug
advanced2: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')
Attempts:
2 left
💡 Hint
The URL requires a parameter but none was given.
✗ Incorrect
The reverse function raises NoReverseMatch when required arguments are missing for URL parameters.
❓ state_output
advanced2: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'})
Attempts:
2 left
💡 Hint
Check the trailing slash in the URL pattern.
✗ Incorrect
The URL pattern ends with a slash, so the reversed URL includes it. The slug is inserted as given.
🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
Think about maintainability and avoiding mistakes.
✗ Incorrect
Using reverse() or {% url %} keeps URLs consistent with URL patterns. If you change a URL pattern, all references update automatically, preventing broken links.