liveBook · Manning

Decorator

As explained in Django in Action

A decorator is a Python language construct that associates additional behavior or metadata with a function or class.

A decorator attaches additional behavior to a function. In a web API, it can also declare what the function represents: Django Ninja uses HTTP-method decorators to turn Django view functions into API endpoints, reducing repetitive endpoint setup. section 11.2 section 11.5

A decorated API endpoint

The @router.get("/") line decorates home and associates it with an HTTP GET route. When a request reaches that route, Ninja calls home(request), and the returned string becomes the response. section 11.2

Decorators can carry configuration as well as behavior. Ninja provides separate decorators for HTTP methods; their arguments can specify a route suffix, response schema, endpoint name, or authentication mechanism. The decorated routes are collected by a Router, registered on one NinjaAPI object, and exposed through api.urls in Django’s URL configuration. section 11.5

When reading or writing a decorated endpoint, check:

  • Choose the decorator matching the HTTP method, such as GET, POST, PUT, PATCH, or DELETE. section 11.5
  • Treat the decorator’s route arguments and the view function’s arguments as a pair: URL values and query parameters are declared through the endpoint definition. section 11.5
  • Keep endpoint grouping and central API registration distinct: Routers collect routes, while NinjaAPI exposes them through Django URLs. section 11.5

How Django in Action covers it

How deep it goes
Explains decorator syntax, route configuration, request flow, and registration, but does not descend into decorator implementation internals.
Examples
Uses a RiffMates hello endpoint and shows Router-to-NinjaAPI-to-Django URL registration.
What you can do after
Apply it via Django Ninja to build routed API endpoints with method, schema, naming, and authentication configuration.
How it connects
Connects decorators with Django Ninja, view functions, HTTP methods, Routers, NinjaAPI, URL routing, and Python type hints.

Also explained in

Compare how 7 books explain decorator →

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.