How does Django match a URL to a view?
How Django URL routing works in urls.py, and what path converters actually do. Django walks urlpatterns from top to bottom and stops at the first pattern that consumes the whole requested path, with no specificity scoring, which is why a broad route placed above a narrow one silently swallows it. Each converter supplies a regex fragment plus topython and tourl, so int delivers a real Python integer to the view and rejects non-digit segments at the routing stage, while str and slug stop at a slash and path crosses it to capture a multi-segment tail. Shows resolve() performing the same lookup a request performs without running a server, an ordering bug where a catch-all captures a literal export route, and reverse() rebuilding URLs from route names and raising NoReverseMatch when an argument fails its converter. Includes runnable Django code and real output, plus re_path, namespaces, trailing slashes, and custom converters.
Комментарии