From eb92467bb057b949c59545bce22284dda2e7aaf0 Mon Sep 17 00:00:00 2001 From: Kent Robin Haugen Date: Wed, 18 Dec 2024 09:26:53 +0100 Subject: [PATCH] Updated docs for url parameters in nested routers --- docs/docs/guides/routers.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/docs/guides/routers.md b/docs/docs/guides/routers.md index 6c772efe6..e4159221b 100644 --- a/docs/docs/guides/routers.md +++ b/docs/docs/guides/routers.md @@ -217,3 +217,38 @@ Now you have the following endpoints: Great! Now go have a look at the automatically generated docs: ![Swagger UI Nested Routers](../img/nested-routers-swagger.png) + +### Nested url parameters + +You can also use url parameters in nested routers by adding `= Path(...)` to the function parameters: + +```python hl_lines="13 16" +from django.contrib import admin +from django.urls import path +from ninja import NinjaAPI, Path, Router + +api = NinjaAPI() +router = Router() + +@api.get("/add/{a}/{b}") +def add(request, a: int, b: int): + return {"result": a + b} + +@router.get("/multiply/{c}") +def multiply(request, c: int, a: int = Path(...), b: int = Path(...)): + return {"result": (a + b) * c} + +api.add_router("add/{a}/{b}", router) + +urlpatterns = [ + path("admin/", admin.site.urls), + path("api/", api.urls), +] +``` + +This will generate the following endpoints: + +``` +/api/add/{a}/{b} +/api/add/{a}/{b}/multiply/{c} +``` \ No newline at end of file