Skills

Django Verification

Verification loop for Django projects: migrations, linting, tests with coverage...

Install

npx skills add xu-xiang/everything-claude-code-zh --skill django-verification -g -a claude-code -y
Category
Testing
Updated
8mo ago
License
MIT
Context cost
Mediumabout 2,042 tokens
Runs scripts
No, instructions only

What it does

Verification loop for Django projects: migrations, linting, tests with coverage, security scans, and deployment readiness checks before release or PR.

Django 验证循环(Verification Loop)

在提交 PR 前、发生重大更改后或部署前运行此循环,以确保 Django 应用的质量与安全性。

阶段 1:环境检查

# 检查 Python 版本
python --version  # 应与项目要求一致

# 检查虚拟环境
which python
pip list --outdated

# 检查环境变量
python -c "import os; import environ; print('DJANGO_SECRET_KEY set' if os.environ.get('DJANGO_SECRET_KEY') else 'MISSING: DJANGO_SECRET_KEY')"

如果环境配置错误,请停止并修正。

阶段 2:代码质量与格式化

# 类型检查
mypy . --config-file pyproject.toml

# 使用 ruff 进行 Lint 检查
ruff check . --fix

# 使用 black 进行格式化
black . --check
black .  # 自动修正

# 导入语句排序
isort . --check-only
isort .  # 自动修正

# Django 特有检查
python manage.py check --deploy

常见问题:

  • 公共函数缺失类型提示(Type Hints)
  • 违反 PEP 8 格式规范
  • 未排序的导入语句
  • 生产环境配置中遗留了调试设置

阶段 3:数据库迁移(Migrations)

# 检查未应用的迁移
python manage.py showmigrations

# 检查并创建缺失的迁移
python manage.py makemigrations --check

# 模拟迁移运行(Dry-run)
python manage.py migrate --plan

# 应用迁移(测试环境)
python manage.py migrate

# 检查迁移冲突
python manage.py makemigrations --merge  # 仅当存在冲突时

报告内容:

  • 待处理迁移数量
  • 迁移冲突情况
  • 缺少迁移的模型更改

阶段 4:测试 + 覆盖率(Coverage)

# 使用 pytest 运行所有测试
pytest --cov=apps --cov-report=html --cov-report=term-missing --reuse-db

# 运行特定应用的测试
pytest apps/users/tests/

# 按标记运行
pytest -m "not slow"  # 跳过耗时较长的测试
pytest -m integration  # 仅运行集成测试

# 查看覆盖率报告
open htmlcov/index.html

报告内容:

  • 测试统计:X 成功,Y 失败,Z 跳过
  • 整体覆盖率:XX%
  • 各应用的覆盖率明细

覆盖率目标:

组件目标
模型 (Models)90%+
序列化器 (Serializers)85%+
视图 (Views)80%+
服务 (Services)90%+
整体80%+

阶段 5:安全扫描

# 依赖项脆弱性检查
pip-audit
safety check --full-report

# Django 安全检查
python manage.py check --deploy

# Bandit 安全 Linter
bandit -r . -f json -o bandit-report.json

# 密钥扫描(如果安装了 gitleaks)
gitleaks detect --source . --verbose

# 环境变量检查
python -c "from django.core.exceptions import ImproperlyConfigured; from django.conf import settings; settings.DEBUG"

报告内容:

  • 发现的有漏洞的依赖项
  • 安全配置问题
  • 检测到的硬编码密钥
  • DEBUG 模式状态(生产环境应为 False)

阶段 6:Django 管理命令

Read the whole SKILL.md on GitHub

More in Testing