Custom Admin Views
==================
Register standalone pages in the admin namespace with ``AdminSite.register_view()``.
Use this for dashboards, reports, or any view outside standard CRUD.
Basic registration
------------------
.. code-block:: python
from fastapi import Request
from fastapi.responses import HTMLResponse
async def dashboard(request: Request) -> HTMLResponse:
return HTMLResponse("""
"""
return HTMLResponse(content=html)
Returning Jinja2 templates
--------------------------
For full template integration, render Jinja2 manually or return an
``HTMLResponse`` with rendered content. Custom views are not automatically
wrapped in the admin base layout — you control the full HTML.
Model-specific URLs
-------------------
``ModelAdmin.get_urls()`` registers extra routes under a model prefix:
.. code-block:: python
class ProductAdmin(ModelAdmin):
def get_urls(self) -> list[tuple[str, Any]]:
return [
("export/", self.export_view),
]
async def export_view(self, request: Request) -> Response:
...
Mounted at: ``/admin/products/export/``
Ecommerce dashboard example
---------------------------
The demo registers a custom dashboard at ``/admin/dashboard/``:
.. code-block:: python
site.register_view("dashboard", "/dashboard/", ecommerce_dashboard)
See ``example/ecommerce/admin.py`` for the full implementation.