reverse('home') return?from django.urls import path, reverse from django.http import HttpResponse urlpatterns = [ path('', lambda request: HttpResponse('Home'), name='home'), ] url = reverse('home')
The URL pattern named 'home' matches the empty string path ''. So reverse('home') returns '/' which is the root URL.
path('article//', view, name='article-detail') , which reverse call correctly builds the URL for id=5?from django.urls import reverse # URL pattern: path('article/<int:id>/', view, name='article-detail')
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.
path('profile//', view, name='profile') , what error occurs when calling reverse('profile') without arguments?from django.urls import reverse # URL pattern: path('profile/<str:username>/', view, name='profile') url = reverse('profile')
The reverse function raises NoReverseMatch when required arguments are missing for URL parameters.
path('blog//', view, name='blog-detail') , what is the value of url after this code?url = reverse('blog-detail', kwargs={'slug': 'my-first-post'})from django.urls import reverse # URL pattern: path('blog/<slug:slug>/', view, name='blog-detail') url = reverse('blog-detail', kwargs={'slug': 'my-first-post'})
The URL pattern ends with a slash, so the reversed URL includes it. The slug is inserted as given.
reverse() or the {% url %} template tag instead of hardcoding URLs in Django projects?Using reverse() or {% url %} keeps URLs consistent with URL patterns. If you change a URL pattern, all references update automatically, preventing broken links.
