Module

app.url_for

URL reversal for named routes.

Builds a{name: Route}index at freeze time and turns names back into path strings at call time. Used byapp.url_for and the {{ url_for(...) }} template global.

Design notes live indocs/rfcs/004-url-for.md.

Functions

build_routes_by_name 1 tuple[Mapping[str, Route…
Return ``(by_name, collisions)`` split from a list of routes. ``by_name`` hold…
def build_routes_by_name(routes: list[Route]) -> tuple[Mapping[str, Route], dict[str, list[Route]]]

Return(by_name, collisions)split from a list of routes.

by_nameholds the first occurrence of each name (a stable read model exposed viaMappingProxyType). Routes that share both nameand path are HTTP method variants of the same URL (e.g. GETfrom page.py plus POST from _actions.py) and are not collisions —url_forreturns the same URL either way.

collisionsmaps any name claimed by routes at different paths to every conflictingRoute — surfaced as a route_namescontract issue.

Parameters
Name Type Description
routes list[Route]
Returns
tuple[Mapping[str, Route], dict[str, list[Route]]]
resolve_url 3 str
Reverse a named route to a URL path. Path-param kwargs substitute into ``{brac…
def resolve_url(routes_by_name: Mapping[str, Route], name: str, /, **params: Any) -> str

Reverse a named route to a URL path.

Path-param kwargs substitute into{braces}and are percent-encoded; any remaining kwargs become a urlencoded query string.Nonevalues in the query are skipped (Starlette convention).

Parameters
Name Type Description
routes_by_name Mapping[str, Route]
name str
**params Any
Returns
str