print(url)?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)
The reverse function uses the named URL pattern and fills in the parameters from kwargs. Here, user_id=42 replaces the placeholder, producing /profile/42/.
post_slug?The correct syntax for a slug parameter is <slug:parameter_name>. Option A uses this correctly.
NoReverseMatch error?urlpatterns = [
path('article/<int:id>/', views.article_detail, name='article-detail'),
]
url = reverse('article-detail', kwargs={'pk': 5})The URL pattern expects a parameter named id, but the reverse call provides pk. This mismatch causes NoReverseMatch.
{% raw %}
# urls.py
urlpatterns = [
path('dashboard/', views.dashboard, name='dashboard'),
]
# template.html
<a href="{% url 'dashboard' %}">Go to Dashboard</a>
{% endraw %}The named URL 'dashboard' corresponds to the path '/dashboard/'. The template tag {% url 'dashboard' %} renders this full path including the trailing slash.
Named URLs let you change the actual URL path in one place (the URLconf) without changing all the code that links to it. This reduces errors and makes updates easier.
