liveBook · Manning

Capture specifier

As explained in Django in Action

A capture specifier is angle-bracket URL syntax that declares a captured value's type and name for a view argument.

A capture specifier is angle-bracket URL syntax that captures part of a path and passes it to a view. It declares both the value's type and the argument name: <int:musician_id> captures an integer named musician_id. section 4.6.1

Common capture types include:

  • str captures any non-empty string except the forward-slash path separator. section 4.6.1
  • int captures an integer and lets Django cast the URL text before calling the view. section 4.6.1
  • slug captures letters, numbers, and hyphens. section 4.6.1

Routing a musician detail page

Register path('musician/<int:musician_id>/', views.musician, name="musician"). Django matches the numeric URL component, passes musician_id to the view, and rejects a non-integer URL as a 404 before the view runs. The view can then retrieve the Musician object and render its template. section 4.6.1

How Django in Action covers it

How deep it goes
Explains syntax, common types, casting, matching, and 404 behavior, but stops short of custom converters.
Examples
Uses a musician detail route, Musician lookup, rendered template, and invalid or nonexistent musician URLs.
What it assumes
Assumes basic Django URL patterns and views while teaching argument capture and automatic URL-value casting.
What you can do after
Build it from scratch by declaring typed URL arguments and receiving them in a Django view.
How it connects
Connects URL routing with views, type casting, object lookup, templates, and HTTP 404 responses.

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.