Django's URL dispatcher and view system form the core of request handling in web applications. This chapter covers everything from basic URL patterns to advanced view techniques, providing the foundation for building robust, scalable web applications.
1. URL Resolution → 2. View Execution → 3. Response Generation
↓ ↓ ↓
Find URL Pattern Execute View Return HttpResponse
Match Parameters Process Logic Render Template
Call View Function Handle Data Send to Browser
URL Dispatcher
View Functions
HTTP Handling
# Project-level URLs (myproject/urls.py)
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
path('api/', include('api.urls')),
path('', include('core.urls')),
]
# App-level URLs (blog/urls.py)
urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:pk>/', views.post_detail, name='post_detail'),
path('create/', views.post_create, name='post_create'),
]
Static Patterns
Dynamic Patterns
Regular Expression Patterns
Characteristics:
Best For:
Request Processing
Response Generation
Data Management
The URL Dispatcher - Pattern matching, parameter extraction, and URL organization Advanced Routing - Namespaces, includes, and complex pattern matching
Function-Based Views - Request handling, response generation, and business logic View Decorators - Authentication, caching, and request modification HTTP Methods - GET, POST, PUT, DELETE handling and RESTful patterns
Rendering Responses - Templates, JSON, and custom content types Redirects and Errors - Status codes, error pages, and user flow File Operations - Uploads, downloads, and media handling
Shortcut Functions - Django helpers for common operations Custom Decorators - Reusable view enhancements Performance Optimization - Caching, database optimization, and scaling
This section builds your URL and view expertise systematically:
Foundation - URL dispatcher mechanics and basic view concepts Implementation - Function-based views and request handling Enhancement - Decorators, shortcuts, and advanced techniques Integration - File handling, HTTP methods, and response types
Each topic includes practical examples, production patterns, and performance considerations that you can implement immediately in your Django projects.
The URL and view system is where your application logic comes to life. Mastering these concepts ensures you can build maintainable, scalable web applications that handle user requests efficiently and provide excellent user experiences.
# Resource-based URLs
urlpatterns = [
path('posts/', views.post_list, name='post_list'), # GET: list, POST: create
path('posts/<int:pk>/', views.post_detail, name='post_detail'), # GET: detail, PUT: update, DELETE: delete
path('posts/<int:pk>/edit/', views.post_edit, name='post_edit'), # GET: form, POST: update
]
# Decorator stacking
@login_required
@require_http_methods(["GET", "POST"])
@cache_page(60 * 15) # 15 minutes
def dashboard(request):
# View logic here
pass
# Multiple response types
def api_endpoint(request):
data = get_data()
if request.accepts('application/json'):
return JsonResponse(data)
else:
return render(request, 'template.html', {'data': data})
Understanding URLs and views is fundamental to Django development. These concepts form the backbone of every Django application, determining how users interact with your system and how your application responds to their requests.
Using Alternative Template Engines
While Django's built-in template engine is powerful and secure, alternative template engines like Jinja2 offer different features and performance characteristics. This chapter covers integrating alternative engines and choosing the right tool for your needs.
The URL Dispatcher
Django's URL dispatcher is a powerful pattern-matching system that maps incoming URLs to view functions. It provides clean, readable URLs while maintaining flexibility for complex routing requirements.