Skip to content
cccskills
FreePublished on GitHub

django-verification

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

The English summary is being prepared. Showing the original description.

Works withPostgreSQLRedis
See how to install

Files included (1)

  • SKILL.md11.1 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

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 管理命令

# 检查模型问题
python manage.py check

# 收集静态文件
python manage.py collectstatic --noinput --clear

# 创建超级用户(如测试需要)
echo "from apps.users.models import User; User.objects.create_superuser('admin@example.com', 'admin')" | python manage.py shell

# 数据库完整性检查
python manage.py check --database default

# 缓存验证(如果使用 Redis)
python -c "from django.core.cache import cache; cache.set('test', 'value', 10); print(cache.get('test'))"

阶段 7:性能检查

# Django Debug Toolbar 输出(检查 N+1 查询)
# 在开发模式下以 DEBUG=True 运行并访问页面
# 在 SQL 面板中查找重复查询

# 查询数量分析
django-admin debugsqlshell  # 如果安装了 django-debug-sqlshell

# 检查缺失的索引
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:静态资源(Static Assets)

# 检查 npm 依赖(如果使用 npm)
npm audit
npm audit fix

# 构建静态文件(如果使用 webpack/vite)
npm run build

# 验证静态文件
ls -la staticfiles/
python manage.py findstatic css/style.css

阶段 9:配置审查(Configuration Review)

# 在 Python Shell 中运行以验证设置
python manage.py shell << EOF
from django.conf import settings
import os

# 关键检查项
checks = {
    'DEBUG is False': not settings.DEBUG,
    'SECRET_KEY set': bool(settings.SECRET_KEY and len(settings.SECRET_KEY) > 30),
    'ALLOWED_HOSTS set': len(settings.ALLOWED_HOSTS) > 0,
    'HTTPS enabled': getattr(settings, 'SECURE_SSL_REDIRECT', False),
    'HSTS enabled': getattr(settings, 'SECURE_HSTS_SECONDS', 0) > 0,
    'Database configured': settings.DATABASES['default']['ENGINE'] != 'django.db.backends.sqlite3',
}

for check, result in checks.items():
    status = '✓' if result else '✗'
    print(f"{status} {check}")
EOF

阶段 10:日志设置

# 测试日志输出
python manage.py shell << EOF
import logging
logger = logging.getLogger('django')
logger.warning('Test warning message')
logger.error('Test error message')
EOF

# 检查日志文件(如果已配置)
tail -f /var/log/django/django.log

阶段 11:API 文档(针对 DRF)

# 生成 Schema
python manage.py generateschema --format openapi-json > schema.json

# 验证 Schema
# 检查 schema.json 是否为有效的 JSON
python -c "import json; json.load(open('schema.json'))"

# 访问 Swagger UI(如果使用 drf-yasg)
# 在浏览器中访问 http://localhost:8000/swagger/

阶段 12:差异审查(Diff Review)

# 显示差异统计
git diff --stat

# 显示实际更改
git diff

# 显示更改的文件名
git diff --name-only

# 检查常见问题
git diff | grep -i "todo\|fixme\|hack\|xxx"
git diff | grep "print("  # 调试语句
git diff | grep "DEBUG = True"  # 调试模式
git diff | grep "import pdb"  # 调试器

检查清单:

  • 无调试语句(print、pdb、breakpoint())
  • 关键代码中无 TODO/FIXME 注释
  • 无硬编码的密钥或凭据
  • 包含模型更改对应的数据库迁移文件
  • 配置更改已文档化
  • 存在外部调用的错误处理逻辑
  • 在必要处进行了事务管理

输出模板

DJANGO 验证报告
==========================

阶段 1:环境检查
  ✓ Python 3.11.5
  ✓ 虚拟环境已激活
  ✓ 所有环境变量已设置

阶段 2:代码质量
  ✓ mypy: 无类型错误
  ✗ ruff: 发现 3 个问题(已自动修正)
  ✓ black: 无格式问题
  ✓ isort: 导入语句已正确排序
  ✓ manage.py check: 无问题

阶段 3:数据库迁移
  ✓ 无未应用的迁移
  ✓ 无迁移冲突
  ✓ 所有模型均有对应迁移

阶段 4:测试 + 覆盖率
  测试:247 成功,0 失败,5 跳过
  覆盖率:
    整体:87%
    users: 92%
    products: 89%
    orders: 85%
    payments: 91%

阶段 5:安全扫描
  ✗ pip-audit: 发现 2 个漏洞(需修复)
  ✓ safety check: 无问题
  ✓ bandit: 无安全问题
  ✓ 未检测到密钥泄露
  ✓ DEBUG = False

阶段 6:Django 命令
  ✓ collectstatic 已完成
  ✓ 数据库完整性 OK
  ✓ 缓存后端可达

阶段 7:性能
  ✓ 未检测到 N+1 查询
  ✓ 数据库索引已配置
  ✓ 查询数量在许可范围内

阶段 8:静态资源
  ✓ npm audit: 无漏洞
  ✓ 资源构建成功
  ✓ 静态文件已收集

阶段 9:配置
  ✓ DEBUG = False
  ✓ SECRET_KEY 已配置
  ✓ ALLOWED_HOSTS 已设置
  ✓ HTTPS 已启用
  ✓ HSTS 已启用
  ✓ 数据库已配置

阶段 10:日志
  ✓ 日志系统已配置
  ✓ 日志文件可写

阶段 11:API 文档
  ✓ Schema 已生成
  ✓ Swagger UI 可访问

阶段 12:差异审查
  更改文件数:12
  +450, -120 行
  ✓ 无调试语句
  ✓ 无硬编码密钥
  ✓ 包含迁移文件

建议:⚠️ 请在部署前修复 pip-audit 发现的漏洞

后续步骤:
1. 更新有漏洞的依赖项
2. 重新运行安全扫描
3. 部署到预发环境(Staging)进行最终测试

部署前检查清单(Deployment Checklist)

  • 所有测试均已通过
  • 覆盖率 ≥ 80%
  • 无安全漏洞
  • 无未应用的迁移
  • 生产环境配置中 DEBUG = False
  • SECRET_KEY 已妥善配置
  • ALLOWED_HOSTS 已正确设置
  • 数据库备份已启用
  • 静态文件已收集并正常提供服务
  • 日志系统已配置并正常运行
  • 错误监控(如 Sentry)已配置
  • CDN 已配置(如适用)
  • Redis/缓存后端已配置
  • Celery Worker 已运行(如适用)
  • HTTPS/SSL 已配置
  • 环境变量已完成文档化

持续集成(CI)

GitHub Actions 示例

# .github/workflows/django-verification.yml
name: Django Verification

on: [push, pull_request]

jobs:
  verify:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:14
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Cache pip
        uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}

      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install ruff black mypy pytest pytest-django pytest-cov bandit safety pip-audit

      - name: Code quality checks
        run: |
          ruff check .
          black . --check
          isort . --check-only
          mypy .

      - name: Security scan
        run: |
          bandit -r . -f json -o bandit-report.json
          safety check --full-report
          pip-audit

      - name: Run tests
        env:
          DATABASE_URL: postgres://postgres:postgres@localhost:5432/test
          DJANGO_SECRET_KEY: test-secret-key
        run: |
          pytest --cov=apps --cov-report=xml --cov-report=term-missing

      - name: Upload coverage
        uses: codecov/codecov-action@v3

快速参考

检查项命令
环境python --version
类型检查mypy .
Lint 检查ruff check .
格式化black . --check
数据库迁移python manage.py makemigrations --check
测试pytest --cov=apps
安全pip-audit && bandit -r .
Django 检查python manage.py check --deploy
静态文件收集python manage.py collectstatic --noinput
差异统计git diff --stat

请记住:自动化验证可以捕获常见问题,但不能替代人工代码审查和在预发环境中的测试。

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

REST API设计模式,包括资源命名、状态码、分页、过滤、错误响应、版本控制和生产API的速率限制。

The English summary is being prepared. Showing the original description.

xu-xiang/everything-claude-code-zh2KUpdated Mar 5, 2026

生产级 API 的 REST API 设计模式,包括资源命名、状态码、分页、过滤、错误响应、版本控制和速率限制。

The English summary is being prepared. Showing the original description.

xu-xiang/everything-claude-code-zh2KUpdated Mar 5, 2026

撰写文章、指南、博客、教程、时事通讯等长篇内容,并根据提供的示例或品牌指南呈现独特的语言风格。当用户需要篇幅超过一段的精炼文字,且重视风格一致性、结构和可信度时,请使用此技能。

The English summary is being prepared. Showing the original description.

xu-xiang/everything-claude-code-zh2KUpdated Mar 5, 2026

编写文章、指南、博客、教程、时事通讯(Newsletter)等长内容,支持从示例或品牌指南中提取独特的语感语调。适用于需要撰写超过一个段落的精炼文本,尤其是对语气一致性、结构和可信度有较高要求时。

The English summary is being prepared. Showing the original description.

xu-xiang/everything-claude-code-zh2KUpdated Mar 5, 2026

编写文章、指南、博客帖子、教程、新闻通讯(newsletter)以及其他长篇内容。这些内容具有从提供的示例或品牌指南中提取出的独特语气。当用户需要比段落更长的精美文案,且对语气一致性、结构和可信度有要求时,请使用此技能(Skill)。

The English summary is being prepared. Showing the original description.

xu-xiang/everything-claude-code-zh2KUpdated Mar 5, 2026

根据提供的示例或品牌指导,以独特的语气撰写文章、指南、博客帖子、教程、新闻简报等长篇内容。当用户需要超过一段的精致书面内容时使用,尤其是当语气一致性、结构和可信度至关重要时。

The English summary is being prepared. Showing the original description.

xu-xiang/everything-claude-code-zh2KUpdated Mar 5, 2026

See all skills by xu-xiang

Report a problem with this skill