year inside the view when the URL /archive/2023/ is accessed?from django.urls import path from django.http import HttpResponse def archive_view(request, year): return HttpResponse(f"Year: {year}") urlpatterns = [ path('archive/<int:year>/', archive_view), ]
int converter converts the URL part to an integer type.The <int:year> converter converts the URL segment to an integer before passing it to the view. So, year is an integer 2023, and the response is Year: 2023.
post_slug.<converter:name>.The correct syntax for a slug converter is <slug:post_slug>. Option B uses str converter which is valid but not slug. Option B and D have incorrect parameter names or missing converter.
path('user//', views.user_profile)
Why does accessing /user/abc123/ return a 404 error?int converter only matches digits.The <int:user_id> converter only matches digits. The string 'abc123' contains letters, so the URL pattern does not match and Django returns 404.
pk in this view?path('item//', views.item_detail)
def item_detail(request, pk):
return HttpResponse(type(pk).__name__ + ' ' + str(pk))
What will be the response content when accessing /item/123e4567-e89b-12d3-a456-426614174000/?uuid converter converts the string to a UUID object.The <uuid:pk> converter converts the URL string to a UUID object. So, pk is a UUID instance, and type(pk).__name__ is 'UUID'.
The slug converter matches ASCII letters, numbers, underscores, and hyphens. The path converter matches any non-empty string including slashes. The int converter matches only positive integers (digits). The uuid converter matches UUID strings with hyphens and both uppercase and lowercase hex digits.