• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

Formal Definition

The URL Dispatcher is Django’s routing mechanism that maps incoming HTTP request paths to view functions or class-based views using a declarative URL configuration (URLconf) composed of path() and re_path() patterns with optional converters, namespaces, and included sub-URLconfs.

Explanation

The URL dispatcher solves the problem of connecting human-readable URLs to application logic. Instead of hardcoding URL-to-view mappings in a single file, Django uses a modular, hierarchical system where each app defines its own URL patterns, which are then included in the project’s root URLconf. This enables namespacing, reversal, and maintainable routing at scale.

How It Works

  1. Request received — HTTP request path extracted from WSGI/ASGI environ
  2. Root URLconf loadedROOT_URLCONF setting points to project’s urls.py
  3. Pattern matching — Iterator through urlpatterns list in order; first match wins
  4. Converter extraction — Path converters (int, str, slug, uuid, path) parse and type-cast URL segments
  5. View resolution — Matched view callable receives request + extracted kwargs
  6. Namespace resolutioninclude() with namespace enables reversible named URLs across apps

Visual Explanation

url_dispatcher Request HTTP Request GET /blog/42/ RootURLconf Root URLconf urls.py Request->RootURLconf 1. Match prefix IncludeBlog include('blog.urls') namespace='blog' RootURLconf->IncludeBlog 2. Delegate to blog/ BlogURLconf Blog URLconf blog/urls.py IncludeBlog->BlogURLconf 3. Strip prefix PathPattern path('post/<int:pk>/',     views.detail,     name='detail') BlogURLconf->PathPattern 4. Match pattern View View Function post_detail(request, pk=42) PathPattern->View 5. Extract pk=42   Call view

Semantic Network

semantic_url_dispatcher THIS URL Dispatcher PRE1 HTTP Request THIS--PRE1 built from PRE2 Regular Expressions THIS--PRE2 built from PRE3 Python Path Converters THIS--PRE3 built from OUT1 Function-Based Views THIS--OUT1 builds into OUT2 Class-Based Views THIS--OUT2 builds into OUT3 URL Reversal THIS--OUT3 builds into OUT4 Namespaced URLs THIS--OUT4 builds into CON1 Flask @route decorator THIS--CON1 contrasts with CON2 FastAPI Path operations THIS--CON2 contrasts with REL1 Middleware (Pre-processing) THIS--REL1 related REL2 REST Framework Routers THIS--REL2 related

Key Properties

  • Order matters: Patterns evaluated top-to-bottom; first match wins
  • Converters: Built-in (int, str, slug, uuid, path) + custom converters via register_converter()
  • Reversal: reverse('name', args=[...]) and {% url 'name' %} generate URLs from names
  • Namespaces: app_name + include(namespace=...) prevent name collisions across apps
  • Lazy evaluation: include() accepts string to avoid circular imports

Connections

Edge Cases & Gotchas

  • Trailing slashes: APPEND_SLASH redirects but can cause POST data loss; be consistent
  • Catch-all patterns: path('<path:resource>/', ...) at end prevents 404s but hides bugs
  • Namespace collisions: Missing app_name in included URLconf breaks reversal
  • Converter precedence: More specific patterns must come before general ones (<int:pk> before <str:slug>)