What is RouteContext and how to use it in controllers
masterThe RouteContext is a central object in Django Ninja Extra that stores request-related information throughout the request lifecycle. It is automatically available within any class inheriting from ControllerBase via the self.context attribute.
Key properties of RouteContext include:
request: The DjangoHttpRequestobject.response: TheHttpResponseobject being built.permission_classes: A list of permission classes applied to the route.args: Positional arguments passed to the route.kwargs: Keyword arguments (route parameters) passed to the route.
from ninja_extra import ControllerBase, api_controller, route
@api_controller("/api")
class MyController(ControllerBase):
@route.get("/example")
def my_method(self):
# Accessing the context via self.context
request = self.context.request
return {"method": request.method}