Guide Home Part 1 - FastAPI Foundations Part 2 - Building Applications Part 3 - Pages and HTML Part 4 - Security and Deployment Part 5 - Going Further Capstone Build Plan Markdown Source All Guides

Chapter 14: Mapping URLs to Pages

Razor Pages map page files to routes. FastAPI maps functions to URLs. To keep page routes maintainable, group them by feature.

src/recipevault/
  web/
    __init__.py
    recipes.py
    account.py
  templates/
    layout.html
    recipes/
      index.html
      detail.html
      edit.html
# web/recipes.py
router = APIRouter(prefix="/recipes", tags=["web-recipes"])


@router.get("")
def index(request: Request):
    ...


@router.get("/{recipe_id}")
def detail(recipe_id: int, request: Request):
    ...

The equivalent of Razor Page route conventions is your project convention. Write it down:

This keeps your web and API surfaces from becoming tangled.