How does URL routing work in Django with urls.py?
Learn how Django URL routing works with urls.py: path(), re_path(), include(), converters, named URLs and reverse resolution, with examples.
Expected Interview Answer
In Django, URL routing maps an incoming request path to a view function using URLpatterns defined in urls.py; Django walks the list top to bottom and dispatches to the first pattern that matches.
Each project has a root URLconf (set by ROOT_URLCONF) whose urlpatterns list holds path() or re_path() entries. path() uses simple converters like <int:id> or <slug:name> to capture segments, while include() delegates a URL prefix to an app's own urls.py so routing stays modular. Captured values are passed to the view as keyword arguments, and naming a pattern lets you reverse it with reverse() or {% url %} instead of hardcoding paths.
- Clean, readable and decoupled URL design
- Reusable per-app URLconfs via include()
- Type-safe capture with path converters
- Reverse URL resolution avoids hardcoded links
- Central place to organize the whole site's routes
AI Mentor Explanation
Think of the umpire's signalling system: each specific hand gesture routes to exactly one outcome — arm out for wide, both arms up for six, finger raised for out. The bowler's delivery is the incoming request, and the umpire scans his known set of signals in order until one matches, then dispatches the correct decision. urls.py is that ordered list of signals, and each view is the outcome the matching pattern triggers.
Step-by-Step Explanation
Step 1
Set the root URLconf
ROOT_URLCONF in settings.py points Django at the project's top-level urls.py to start routing.
Step 2
Define urlpatterns
Build a list of path() and re_path() entries, each mapping a route string to a view callable.
Step 3
Capture parameters
Use converters like <int:id> or <slug:name> to capture URL segments and pass them to the view as kwargs.
Step 4
Include app routes
Delegate a prefix to an app with include('app.urls') so each app owns its own URLconf.
Step 5
Name and reverse
Give patterns a name= and resolve them with reverse() or {% url %} instead of hardcoding paths.
What Interviewer Expects
- Knows path() vs re_path() and when to use each
- Understands include() for modular app routing
- Can explain path converters and captured kwargs
- Understands named URLs and reverse resolution
- Knows patterns are matched top to bottom
Common Mistakes
- Forgetting order matters — a broad pattern shadows later specific ones
- Hardcoding URLs instead of using reverse() or {% url %}
- Confusing path() converter syntax with regex from re_path()
- Omitting the trailing include() prefix or app_name namespace
- Assuming query string params are part of the URL pattern
Best Answer (HR Friendly)
“Django uses a file called urls.py that acts like a directory, matching each web address a visitor types to the piece of code that should respond. It checks the list of routes from top to bottom and runs the first one that fits.”
Code Example
# project/urls.py
from django.urls import path, include
urlpatterns = [
path('blog/', include('blog.urls')),
]
# blog/urls.py
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.post_list, name='list'),
path('<int:pk>/', views.post_detail, name='detail'),
]Follow-up Questions
- What is the difference between path() and re_path()?
- How does include() help structure a large project?
- How do you reverse a named URL in a template and in Python?
- What are URL namespaces and app_name used for?
- How does Django decide which pattern wins when two could match?
MCQ Practice
1. Which function is used to delegate a URL prefix to another app's urls.py?
include() attaches an app's URLconf under a prefix, keeping routing modular per app.
2. In path('<int:id>/', ...), what does <int:id> do?
The int converter captures a numeric segment and passes it to the view as the keyword argument id.
3. How are urlpatterns evaluated for a request?
Django scans urlpatterns in order and dispatches to the first pattern that matches the request path.
Flash Cards
What sets the root URLconf? — The ROOT_URLCONF setting, which points to the project's top-level urls.py.
path() vs re_path()? — path() uses simple converters like <int:id>; re_path() matches full regular expressions.
What does include() do? — Delegates a URL prefix to another app's urls.py for modular routing.
Why name URL patterns? — So you can reverse them with reverse() or {% url %} instead of hardcoding paths.