
关于
Django 项目的验证循环:迁移、代码检查、带覆盖率的测试、安全扫描和发布或 PR 前的部署就绪检查。
name: django-verification description: "Django 项目验证循环:迁移、代码检查、带覆盖率的测试、安全扫描,以及发布或 PR 前的部署就绪检查。" origin: ECC
Django 验证循环
在 PR 前、重大变更后和部署前运行,确保 Django 应用的质量和安全性。
何时激活
- 在为 Django 项目提交 Pull Request 之前
- 在重大模型变更、迁移更新或依赖升级之后
- 预发布或生产环境的部署前验证
- 运行完整的 环境 → 代码检查 → 测试 → 安全 → 部署就绪 流水线
- 验证迁移安全性和测试覆盖率
阶段 1:环境检查
# Verify Python version
python --version # Should match project requirements
# Check virtual environment
which python
pip list --outdated
# Verify environment variables
python -c "import os; import environ; print('DJANGO_SECRET_KEY set' if os.environ.get('DJANGO_SECRET_KEY') else 'MISSING: DJANGO_SECRET_KEY')"
如果环境配置有误,停止并修复。
阶段 2:代码质量与格式化
# Type checking
mypy . --config-file pyproject.toml
# Linting with ruff
ruff check . --fix
# Formatting with black
black . --check
black . # Auto-fix
# Import sorting
isort . --check-only
isort . # Auto-fix
# Django-specific checks
python manage.py check --deploy
常见问题:
- 公共函数缺少类型注解
- PEP 8 格式违规
- 导入未排序
- 调试设置遗留在生产配置中
阶段 3:迁移
# Check for unapplied migrations
python manage.py showmigrations
# Create missing migrations
python manage.py makemigrations --check
# Dry-run migration application
python manage.py migrate --plan
# Apply migrations (test environment)
python manage.py migrate
# Check for migration conflicts
python manage.py makemigrations --merge # Only if conflicts exist
报告:
- 待处理迁移数量
- 任何迁移冲突
- 没有对应迁移的模型变更
阶段 4:测试 + 覆盖率
# Run all tests with pytest
pytest --cov=apps --cov-report=html --cov-report=term-missing --reuse-db
# Run specific app tests
pytest apps/users/tests/
# Run with markers
pytest -m "not slow" # Skip slow tests
pytest -m integration # Only integration tests
# Coverage report
open htmlcov/index.html
报告:
- 总测试数:X 通过,Y 失败,Z 跳过
- 总体覆盖率:XX%
- 按应用分解的覆盖率
覆盖率目标:
| 组件 | 目标 | |-----------|--------| | 模型 | 90%+ | | 序列化器 | 85%+ | | 视图 | 80%+ | | 服务 | 90%+ | | 总体 | 80%+ |
阶段 5:安全扫描
# Dependency vulnerabilities
pip-audit
safety check --full-report
# Django security checks
python manage.py check --deploy
# Bandit security linter
bandit -r . -f json -o bandit-report.json
# Secret scanning (if gitleaks is installed)
gitleaks detect --source . --verbose
# Environment variable check
python -c "from django.core.exceptions import ImproperlyConfigured; from django.conf import settings; settings.DEBUG"
报告:
- 发现的有漏洞依赖
- 安全配置问题
- 检测到的硬编码密钥
- DEBUG 模式状态(生产环境应为 False)
阶段 6:Django 管理命令
# Check for model issues
python manage.py check
# Collect static files
python manage.py collectstatic --noinput --clear
# Create superuser (if needed for tests)
echo "from apps.users.models import User; User.objects.create_superuser('admin@example.com', 'admin')" | python manage.py shell
# Database integrity
python manage.py check --database default
# Cache verification (if using Redis)
python -c "from django.core.cache import cache; cache.set('test', 'value', 10); print(cache.get('test'))"
阶段 7:性能检查
# Django Debug Toolbar output (check for N+1 queries)
# Run in dev mode with DEBUG=True and access a page
# Look for duplicate queries in SQL panel
# Query count analysis
django-admin debugsqlshell # If django-debug-sqlshell installed
# Check for missing indexes
python manage.py shell << EOF
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("SELECT table_name, index_name FROM information_schema.statistics WHERE table_schema = 'public'")
print(cursor.fetchall())
EOF
报告:
- 每页查询数量(典型页面应少于 50 次)
- 缺失的数据库索引
- 检测到的重复查询
阶段 8:静态资源
# Check for npm dependencies (if using npm)
npm audit
npm audit fix
# Build static files (if using webpack/vite)
npm run build
# Verify static files
ls -la staticfiles/
python manage.py findstatic css/style.css
阶段 9:配置审查
# Run in Python shell to verify settings
python manage.py shell << EOF
from django.conf import settings
import os
# Critical checks
checks = {
'DEBUG is False': n```
兼容工具
Claude CodeCursor
标签
测试

