URL Router¶
This page covers how the file router scans the filesystem, builds URL patterns, and reloads at runtime.
Overview¶
The URL subsystem owns the file router, the filesystem walker that discovers page modules, and the reverse helpers.
It listens on the Django URL resolver through include("next.urls") and produces patterns from the filesystem layout.
Pipeline¶
flowchart TB
Walk[Filesystem walk] --> Parser[Parser]
Parser --> Patterns[URL patterns]
Patterns --> Manager[RouterManager]
Manager --> Resolver[Django URL resolver]
Resolver --> PageView[Page view]
Manager -- reload --> Reload[Rebuild patterns]
Reload --> Patterns
Modules¶
next.urls.backends.RouterBackendis the abstract contract.FileRouterBackendimplements file based routing.RouterFactorylooks up backends by dotted path.next.urls.parser.Turns directory names into URL patterns. Recognises
[name],[type:name], and[[name]]shapes.next.urls.manager.RouterManagerbuilds the active pattern list, exposesreload, and emits therouter_reloadedsignal.next.urls.dispatcher.FilesystemTreeDispatcherwalks the pages directory tree and yields(url_path, page_file)pairs that the router turns into URL patterns. The module-level helperscan_pages_treeinstantiates the dispatcher with the configured skip set and returns the same pairs as an iterator.next.urls.markers.Hosts the
DUrlandDQueryannotation markers, the four request/URL/query parameter providers, and theget_multi_valueshelper. See Dependency Injection for the marker semantics and the provider order.next.urls.reverse.page_reverseandwith_queryhelpers.
URL Name Computation¶
Names follow next:page_<segments> where the segments come from the directory path.
Static segments contribute their directory name unchanged.
Captured segments contribute their parameter name without the type prefix.
Wildcard segments contribute the parameter name without brackets.
The template URL_NAME_TEMPLATE controls the format.
The default page_{name} produces the names listed in File Router.
Reload Mechanics¶
router_manager.reload() does three things in order.
Rebuilds the backend list from
DEFAULT_PAGE_BACKENDS.Clears the Django URL resolver cache.
Emits the
router_reloadedsignal.
The next request observes the new patterns without a process restart. Long lived processes such as websocket subscribers listen for the signal to refresh cached URL references.
Multiple Backends¶
The settings list accepts more than one backend. Each backend reports its own list of patterns. The Django URL resolver checks them in order and the first match wins.
If two routes resolve to the same Django path the next.E015 system check reports the conflict at startup, whether they come from one tree or several.
Extension Points¶
Subclass
RouterBackendto feed the resolver from a different source, or subclassFileRouterBackendto add patterns or augment naming on the file-based backend.Register a custom backend in
RouterFactoryand reference it through the settings dotted path.Subscribe to
route_registeredto observe each new pattern. It fires once per discovered pattern withsender=FileRouterBackendand theurl_pathandfile_pathkeyword arguments. See Signals Reference.
See Also¶
See also
File Router for the topic guide. URL Reversing for the reverse helpers. Autoreload for the reload mechanics tied to the watcher.