What is URL Routing?
URL routing maps URLs to view functions. Django uses URL patterns to determine which view handles each request based on the URL path.
Basic URL Pattern
Define URL patterns in urls.py:
# myproject/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
path('contact/', views.contact, name='contact'),
]
URL Parameters
Capture values from URLs:
# Integer parameter
path('user/<int:user_id>/', views.user_detail, name='user_detail'),
# String parameter
path('user/<str:username>/', views.user_profile, name='user_profile'),
# Slug parameter
path('post/<slug:slug>/', views.post_detail, name='post_detail'),
# UUID parameter
path('item/<uuid:item_id>/', views.item_detail, name='item_detail'),
# Path parameter (includes slashes)
path('files/<path:filepath>/', views.serve_file, name='serve_file'),
Multiple Parameters
Capture multiple values:
path('blog/<int:year>/<int:month>/<slug:slug>/',
views.blog_post,
name='blog_post'),
# View receives: year, month, slug
def blog_post(request, year, month, slug):
# Use parameters
pass
URL Names
Name URLs for easy reference:
# In urls.py
path('products/', views.product_list, name='product_list'),
# In templates
<a href="{% url 'product_list' %}">Products</a>
# In views
from django.shortcuts import redirect
return redirect('product_list')
# With parameters
path('product/<int:id>/', views.product_detail, name='product_detail')
<a href="{% url 'product_detail' id=product.id %}">View</a>
Including Other URL Configs
Organize URLs with includes:
# myproject/urls.py
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
path('shop/', include('shop.urls')),
]
# blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.blog_list, name='blog_list'),
path('<slug:slug>/', views.blog_detail, name='blog_detail'),
]
Regular Expression Patterns
Use regex for complex patterns:
from django.urls import re_path
# Match specific pattern
re_path(r'^articles/(?P<year>[0-9]{4})/$', views.article_year),
# Match optional parameter
re_path(r'^archive/(?P<year>[0-9]{4})?/$', views.archive),
URL Patterns Order
Order matters - more specific patterns first:
# Correct order
urlpatterns = [
path('blog/new/', views.new_post, name='new_post'), # Specific first
path('blog/<slug:slug>/', views.post_detail, name='post_detail'), # General
]
# Wrong order (would never match 'new')
urlpatterns = [
path('blog/<slug:slug>/', views.post_detail), # Matches 'new' first!
path('blog/new/', views.new_post),
]
Best Practices
- Use descriptive names: Clear URL pattern names
- Name all URLs: Makes templates and redirects easier
- Organize with includes: Separate URLs by app
- Order matters: More specific patterns first
- Use path() over re_path(): Simpler and more readable
Next Steps
Learn about Django Views to handle requests, or explore templates for rendering responses.