A URL argument is a value captured from a URL path and passed to a view or API function. It lets one handler serve many objects: rather than creating a page for every musician, a route can carry the musician’s database ID. URL arguments usually identify a target object; query parameters more often modify a result or enable optional behavior. section 4.6 section 11.2.2
Figure 4.5 URL-based arguments and query parameters
Compare a path value with query parameters: the path selects what is being addressed, while a value after ? can adjust the request. section 4.6
A Django detail route follows a short request flow:
- Declare a named, typed capture in a path pattern, such as musician/<int:musician_id>/; Django passes the captured value to the matching view argument. section 4.6.1
- Look up the model object with the captured ID, commonly through get_object_or_404(Musician, id=musician_id). section 4.6.1
- Render the found object through a template. If no single object matches, get_object_or_404() raises Http404 and Django returns a 404 page instead of continuing the view. section 4.6.1
Listing 4.5 App specific URL pattern declaration file for bands
# RiffMates/bands/urls.py
from django.urls import path
from bands import views
urlpatterns = [
path('musician/<int:musician_id>/', views.musician, name="musician"), #1
]
The angle-bracket declaration names the captured value and constrains it to an integer; that name must match the view’s argument. section 4.6.1
Fetching one API object
A Ninja endpoint uses braces rather than Django path angle brackets: /promoter/{promoter_id}/ binds promoter_id to the function argument. The function retrieves that Promoter by ID and returns it. A similar venue endpoint returns a serialized object whose URL can be built with reverse() from the venue ID. section 11.2.2 section 11.2.2
