Django框架
Django是Python生态系统中最流行的Web框架之一,以"完备的"、"高级的"和"Python的"为特点,采用了"电池已包含"的设计理念,提供了Web开发所需的大部分组件。Django最初由Lawrence Journal-World的Web团队设计和开发,用于管理多个新闻网站,现已成为开源社区维护的项目。
Django核心概念
MVT架构
Django采用MVT(Model-View-Template)架构,这是对传统MVC(Model-View-Controller)架构的变种:
- Model(模型):定义数据结构和数据库交互逻辑
- View(视图):处理HTTP请求并返回HTTP响应
- Template(模板):定义用户界面的表现层
┌────────────┐ ┌────────────┐ ┌────────────┐
│ │ │ │ │ │
│ Model │◄───┤ View │◄───┤ Template │
│ │ │ │ │ │
└────────────┘ └────────────┘ └────────────┘
│ ▲ ▲
│ │ │
▼ │ │
┌────────────┐ │ │
│ │ │ │
│ Database │ │ │
│ │ │ │
└────────────┘ │ │
│ │
┌────────────┐ │
│ │ │
│ User │──────────┘
│ │
└────────────┘
项目结构
典型的Django项目结构如下:
myproject/
├── manage.py # 项目管理命令行工具
├── myproject/ # 项目配置包
│ ├── __init__.py # Python包标识
│ ├── settings.py # 项目设置
│ ├── urls.py # URL声明
│ ├── asgi.py # ASGI兼容Web服务器入口
│ └── wsgi.py # WSGI兼容Web服务器入口
└── myapp/ # 应用包
├── __init__.py # Python包标识
├── admin.py # 管理后台配置
├── apps.py # 应用配置
├── migrations/ # 数据库迁移文件
│ └── __init__.py
├── models.py # 数据模型
├── tests.py # 测试代码
├── views.py # 视图函数
└── templates/ # 模板文件
└── myapp/
└── index.html
Django主要功能
ORM系统
Django的对象关系映射(ORM)系统允许开发者使用Python代码定义数据模型,而不必直接编写SQL:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
def __str__(self):
return self.name
class Book(models.Model):
title = models.CharField(max_length=200)
publication_date = models.DateField()
author = models.ForeignKey(Author, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=6, decimal_places=2)
pages = models.IntegerField()
def __str__(self):
return self.title
ORM提供了以下功能:
- 数据模型定义:使用Python类定义数据表结构
- 数据查询API:提供丰富的查询方法而不需要写SQL
- 关系管理:处理一对一、一对多、多对多关系
- 事务支持:确保数据完整性
URL路由
Django的URL路由系统用于映射URL模式到视图函数:
# urls.py
from django.urls import path
from . import views
urlpatterns = [
path('books/', views.book_list, name='book_list'),
path('books/<int:pk>/', views.book_detail, name='book_detail'),
path('books/create/', views.book_create, name='book_create'),
path('books/<int:pk>/update/', views.book_update, name='book_update'),
path('books/<int:pk>/delete/', views.book_delete, name='book_delete'),
]
路由系统支持:
- URL命名:便于在模板和视图中引用
- 参数捕获:从URL中提取参数
- 正则表达式:通过
re_path
支持复杂的URL模式 - 包含和嵌套:通过
include
组织大型项目的URL结构
视图系统
Django视图处理Web请求并返回Web响应:
# Function-based view
from django.shortcuts import render, get_object_or_404, redirect
from .models import Book
from .forms import BookForm
def book_list(request):
books = Book.objects.all()
return render(request, 'books/book_list.html', {'books': books})
def book_detail(request, pk):
book = get_object_or_404(Book, pk=pk)
return render(request, 'books/book_detail.html', {'book': book})
# Class-based view
from django.views.generic import ListView, DetailView
from .models import Book
class BookListView(ListView):
model = Book
template_name = 'books/book_list.html'
context_object_name = 'books'
class BookDetailView(DetailView):
model = Book
template_name = 'books/book_detail.html'
context_object_name = 'book'
Django支持两种类型的视图:
- 函数视图(FBV):使用函数定义视图
- 类视图(CBV):使用类定义视图,提供可复用的行为
模板系统
Django的模板系统提供了一种控制内容显示的方式:
<!-- book_list.html -->
<!DOCTYPE html>
<html>
<head>
<title>书籍列表</title>
</head>
<body>
<h1>书籍列表</h1>
{% if books %}
<ul>
{% for book in books %}
<li>
<a href="{% url 'book_detail' book.id %}">{{ book.title }}</a>
- {{ book.author.name }}
</li>
{% endfor %}
</ul>
{% else %}
<p>没有可用的书籍。</p>
{% endif %}
</body>
</html>
模板系统特性:
- 变量替换:使用
语法
- 标签:使用
{% tag %}
语法执行逻辑操作 - 过滤器:使用
0
修改变量显示 - 模板继承:通过
{% extends %}
和{% block %}
创建基础模板 - 包含:通过
{% include %}
复用模板片段
表单处理
Django的表单系统简化了表单处理和验证:
# forms.py
from django import forms
from .models import Book
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'publication_date', 'price', 'pages']
widgets = {
'publication_date': forms.DateInput(attrs={'type': 'date'}),
}
# views.py
def book_create(request):
if request.method == 'POST':
form = BookForm(request.POST)
if form.is_valid():
book = form.save()
return redirect('book_detail', pk=book.pk)
else:
form = BookForm()
return render(request, 'books/book_form.html', {'form': form})
表单系统功能:
- 自动生成HTML:根据字段定义生成表单元素
- 数据验证:验证用户输入的数据
- 错误处理:收集和显示验证错误
- CSRF保护:内置跨站请求伪造保护
管理后台
Django的管理后台是一个自动生成的界面,用于管理网站内容:
# admin.py
from django.contrib import admin
from .models import Author, Book
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ('name', 'email')
search_fields = ('name', 'email')
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ('title', 'author', 'publication_date', 'price')
list_filter = ('publication_date', 'author')
search_fields = ('title',)
date_hierarchy = 'publication_date'
管理后台特性:
- 自动CRUD:创建、读取、更新、删除操作
- 权限管理:用户、组和权限控制
- 可定制:可以自定义管理界面的显示和行为
- 批量操作:批量修改或删除记录
认证系统
Django提供了完整的用户认证系统:
# views.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserCreationForm()
return render(request, 'registration/register.html', {'form': form})
@login_required
def profile(request):
return render(request, 'profile.html')
认证系统功能:
- 用户账户:用户创建、登录、登出
- 权限:定义用户可以执行的操作
- 分组:将权限分配给用户组
- 密码管理:安全的密码存储和重置
Django高级功能
中间件
中间件是处理请求/响应周期的钩子系统:
# middleware.py
class SimpleMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# 在视图之前的代码
# ...
response = self.get_response(request)
# 在视图之后的代码
# ...
return response
常见中间件用途:
- 认证:验证用户身份
- 安全:提供安全特性如XSS保护
- 会话管理:处理用户会话
- CSRF保护:防止跨站请求伪造
信号系统
信号允许在某些事件发生时发送通知:
# signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from .models import Profile
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
常见信号:
- model信号:模型实例的创建、更新、删除
- 请求信号:请求开始和结束
- 管理信号:用户登录、登出等认证事件
缓存系统
Django的缓存系统提高了应用性能:
# settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/1',
}
}
# views.py
from django.views.decorators.cache import cache_page
@cache_page(60 * 15) # 缓存15分钟
def my_view(request):
# ...
return response
支持的缓存后端:
- 内存缓存:存储在进程内存中
- 数据库缓存:存储在数据库表中
- 文件系统缓存:存储在文件系统中
- Memcached:使用Memcached服务
- Redis:使用Redis服务
REST框架集成
Django REST Framework(DRF)是Django的强大扩展,用于构建API:
# serializers.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'title', 'author', 'publication_date', 'price']
# views.py
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
# urls.py
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register(r'books', views.BookViewSet)
urlpatterns = [
path('api/', include(router.urls)),
]
DRF提供的功能:
- 序列化:转换复杂数据为Python原生数据类型
- 视图集:基于类的API视图
- 路由:自动URL路由
- 认证:多种认证方式
- 权限:精细的权限控制
- 分页:自动分页支持
- 过滤:查询过滤支持
Django性能优化
数据库优化
索引:为常用查询字段添加索引
pythonclass Book(models.Model): title = models.CharField(max_length=200, db_index=True) # ...
选择合适的字段类型:使用合适的字段类型减少存储空间和提高查询效率
查询优化:使用
select_related
和prefetch_related
减少数据库查询python# 避免N+1查询问题 books = Book.objects.select_related('author').all()
数据库连接池:使用连接池管理数据库连接
缓存策略
视图缓存:缓存整个视图的输出
python@cache_page(60 * 15) def book_list(request): # ...
模板片段缓存:缓存模板的特定部分
html{% load cache %} {% cache 600 sidebar %} <!-- 侧边栏内容 --> {% endcache %}
低级缓存API:对特定操作进行细粒度缓存
pythonfrom django.core.cache import cache def get_book_count(): count = cache.get('book_count') if count is None: count = Book.objects.count() cache.set('book_count', count, 3600) # 缓存1小时 return count
静态文件处理
- 使用CDN:将静态文件部署到内容分发网络
- 文件压缩:压缩CSS和JavaScript文件
- 缓存控制:设置适当的HTTP缓存头
Django测试
单元测试
# tests.py
from django.test import TestCase
from django.urls import reverse
from .models import Author, Book
class BookModelTest(TestCase):
def setUp(self):
self.author = Author.objects.create(name="Test Author", email="test@example.com")
self.book = Book.objects.create(
title="Test Book",
author=self.author,
publication_date="2023-01-01",
price=29.99,
pages=200
)
def test_book_creation(self):
self.assertEqual(self.book.title, "Test Book")
self.assertEqual(self.book.author.name, "Test Author")
class BookViewTest(TestCase):
def setUp(self):
# 同上...
def test_book_list_view(self):
url = reverse('book_list')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Test Book")
集成测试
# tests.py
class BookIntegrationTest(TestCase):
def setUp(self):
self.author = Author.objects.create(name="Test Author", email="test@example.com")
self.client = Client()
def test_book_creation_flow(self):
# 登录
self.client.login(username='admin', password='password')
# 创建书籍
url = reverse('book_create')
data = {
'title': 'New Book',
'author': self.author.id,
'publication_date': '2023-01-01',
'price': '19.99',
'pages': '100'
}
response = self.client.post(url, data)
# 验证重定向
self.assertEqual(response.status_code, 302)
# 验证书籍已创建
self.assertTrue(Book.objects.filter(title='New Book').exists())
Django部署
WSGI/ASGI服务器
Gunicorn:轻量级WSGI服务器
bashgunicorn myproject.wsgi:application
uWSGI:高性能WSGI服务器
bashuwsgi --http :8000 --module myproject.wsgi
Daphne:ASGI服务器,支持WebSocket
bashdaphne myproject.asgi:application
静态文件
生产环境中的静态文件处理:
# settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# 收集静态文件
# python manage.py collectstatic
容器化部署
使用Docker部署Django应用:
# Dockerfile
FROM python:3.9
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY .. .
RUN python manage.py collectstatic --noinput
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "myproject.wsgi"]
# docker-compose.yml
version: '3'
services:
db:
image: postgres:13
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env
web:
build: .
command: gunicorn myproject.wsgi:application --bind 0.0.0.0:8000
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
env_file:
- ./.env
volumes:
postgres_data:
Django最佳实践
- 项目结构:保持清晰的项目结构,将应用分解为可重用的组件
- 配置管理:使用环境变量和配置文件分离不同环境的配置
- 代码质量:使用类型提示、文档字符串和自动化测试保证代码质量
- 安全最佳实践:遵循Django安全建议,定期更新依赖
- 数据库迁移:使用迁移系统管理数据库架构变更
- 使用类视图:利用类视图提高代码复用性
- API设计:使用Django REST Framework设计RESTful API
- 缓存策略:实施适当的缓存策略提高性能
Django生态系统
常用第三方包
- django-crispy-forms:改进表单渲染
- django-filter:复杂的查询过滤
- django-debug-toolbar:开发调试工具
- django-allauth:高级认证功能
- django-cors-headers:跨域资源共享
扩展Django管理后台
- django-grappelli:增强管理界面
- django-admin-interface:自定义管理界面
- django-import-export:导入导出数据
文件和媒体处理
- django-storages:多种存储后端支持
- Pillow:图像处理
Django作为一个成熟且功能全面的Web框架,为开发者提供了从数据库操作到模板渲染的全套工具,适合从小型项目到大型企业级应用的各种场景。通过遵循Django的设计理念和最佳实践,开发者可以构建安全、可扩展和易于维护的Web应用。