liveBook · Manning

URL argument

As explained in Django in Action

A URL argument is a value captured from part of a URL and passed as an argument to a view.

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
figure

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

How Django in Action covers it

How deep it goes
Walks through route syntax, model lookup, template rendering, and 404 handling; API examples add route binding and serialized responses.
Examples
Demonstrates musician and promoter ID routes, a venue endpoint, rendered musician details, and an absent musician ID returning 404.
What it assumes
Teaches URL capture notation and lookup flow from zero while relying on models with unique IDs and callable views or endpoints.
What you can do after
Build it from scratch: route an ID into a view or endpoint, retrieve its model, return rendered or serialized data, and handle misses.
How it connects
Connects URL patterns, query parameters, Django ORM lookups, get_object_or_404(), templates, API endpoints, serializers, slugs, and reverse().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.