Django URLconf Design: Connect Views with Named Routes
Django’s URLconf maps URL paths to views. Django checks patterns in order and calls the first match, so explicit path converters and named URLs make request routing easier to read and safer to change. Keep the responsibilities clear: URLconfs connect paths to views, while views handle the response or application work.
Django’s URLconf maps URL paths to views. Django checks patterns in order and calls the first match, so explicit path converters and named URLs make request routing easier to read and safer to change. Keep the responsibilities clear: URLconfs connect paths to views, while views handle the response or application work.
How a request reaches a view
By default, Django looks for urlpatterns in the module named by ROOT_URLCONF. It checks the patterns from top to bottom, selects the first match for the requested path, and passes the view an HttpRequest plus captured path arguments. If no pattern matches, or an exception occurs during handling, Django uses an error-handling view.
Suppose the project URLconf includes polls/ from polls.urls, and the app URLconf declares path('int:question_id/', views.detail, name='detail'). A request to /polls/34/ then calls detail with question_id=34. The int:... converter accepts a numeric path segment and supplies it as an integer. Do not start a path() pattern with a slash. URLconfs match the path, not the domain or query string.
Order affects behavior. Put a special case before a broader pattern if it must match first. If a request reaches an unexpected view, check whether an earlier, broader pattern captures it. Also check trailing slashes: a pattern requiring a final slash may not match /articles/2003.
Choose app boundaries and converters
As a feature grows, split routes into app URLconfs and connect them with include() instead of placing every route in the project URLconf. Django removes the matched parent prefix and resolves the remaining path in the included URLconf. For example, if the project includes polls/, polls.urls can define the empty path and int:question_id/ beneath it.
Built-in converters include int, str, slug, uuid, and path. A converter determines both which string shape matches and which Python value reaches the view: int supplies an integer, while uuid supplies a UUID object. Use path when a value may contain slashes, and make sure the view expects multiple path segments in that value. Narrow converters reject malformed addresses earlier.
Naming a URL avoids copying path strings into templates and code. Give a pattern name='detail', then build its URL by name, for example {% url 'polls:detail' question.id %} in a template or reverse('polls:detail', args=[question.id]) in Python. If you change the path but keep the name and argument contract, fewer link locations need edits. Names may collide across apps, so use app_name and a namespace to qualify them.
Choose a function-based or class-based view
A function-based view keeps input and output visible in one place. It often suits a short flow, clear branching, or a specialized response. A class-based view can reuse behavior through inheritance or mixins, and Django’s generic views provide recurring patterns such as lists and details. TemplateView can serve a single template; connect a class-based view in a URLconf with as_view().
Choosing a class-based view does not automatically make a design shorter or better. If changing behavior requires tracing several inheritance layers and mixins, a function-based view may be easier to read. If multiple screens repeat the same structure, consider a generic view. Separating behavior by HTTP method can also make a class-based view useful.
Check the wiring with a small URLconf
Suppose an app URLconf defines list and detail routes like this:
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
]The project URLconf connects the app with path('polls/', include('polls.urls')). The detail view’s parameter name must match question_id in the route, and links should use the namespaced pattern name. This example illustrates the documented routing mechanics; it does not claim that project files were created or run.
Reproduce and diagnose routing problems
Confirm that ROOT_URLCONF in the project settings points to the expected module.
Check that the project route and the app’s urlpatterns are connected with the correct include().
Review pattern order from top to bottom and see whether an earlier pattern captures the request.
Check path values and converters. For example, <int:id> expects digits.
Confirm that the view returns an HttpResponse (or subclass) or raises a supported exception such as Http404.
Use namespaced URL names rather than assembling path strings in templates or Python.
Request the expected path with its arguments in the development server and compare behavior against the documentation and configuration for the installed Django version.
When matching fails, compare pattern order, included prefixes, converters, and view parameter names one at a time. If the route still does not match, inspect ROOT_URLCONF and whether middleware changes request.urlconf. After a fix, verify that direct requests and reverse() or template URL generation resolve to the same pattern; this reduces regressions when paths change.
References
[Django 6.0: URL dispatcher](https://docs.djangoproject.com/en/6.0/topics/http/urls/)
[Django 6.0: Writing your first Django app, part 3](https://docs.djangoproject.com/en/6.0/intro/tutorial03/)
[Django 6.0: Class-based views](https://docs.djangoproject.com/en/6.0/topics/class-based-views/)
[Django 6.0: Writing views](https://docs.djangoproject.com/en/6.0/topics/http/views/)
[Django 6.0: Shortcut functions](https://docs.djangoproject.com/en/6.0/topics/http/shortcuts/)