Token导航 LogoToken导航TokenDH.com
研究检索敏感数据github未标认证来源可访问许可证需确认审计通过

djangoDjango 测试

Agent Skill

用于辅助 Python 项目开发、测试、依赖管理和常见框架工作流。它适合让 Agent 阅读 Python 代码、定位测试问题、整理运行命令、生成脚本或分析数据处理逻辑。使用时需要确认项目虚拟环境、依赖版本和测试入口;涉及执行脚本、读写文件、访问数据库或调用外部 API 时,应先明确运行目录和输入输出范围,避免误改生产数据。

总安装

282

周安装

12

GitHub Stars

8

下载量

99
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:django(Django 测试)
来源仓库:https://github.com/ar4mirez/samuel
仓库路径:skills/django
安装命令:
npx skills add https://github.com/ar4mirez/samuel --skill django
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/ar4mirez/samuel --skill django

简介

Django 5+ 全栈 Web 应用开发完整实践指南。

  • 内置 ORM、认证与安全机制开箱即用。适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。
  • 遵循胖模型瘦视图原则集中业务逻辑处理。
  • 所有数据库查询必须显式声明关联关系。django 属于研究检索类 Skill,可作为该场景下的辅助能力补充。
  • CSRF/XSS 防护默认开启无需额外配置。

SKILL.md

Django Framework Guide

Applies to: Django 5+, Django REST Framework, Django Channels Language: Python 3.10+

Core Principles

  1. Batteries Included: Leverage Django's built-in features before adding third-party packages
  2. DRY: Don't Repeat Yourself -- use abstract models, mixins, and shared utilities
  3. Fat Models, Thin Views: Keep business logic in models and services, not views
  4. Explicit Over Implicit: Use clear URL patterns, explicit imports, named relationships
  5. Security by Default: CSRF, XSS protection, SQL injection prevention are built in

When to Use Django

Good fit:

  • Full-stack web applications with templates
  • Admin interface needed out of the box
  • Content management systems
  • Session-based authentication
  • ORM with built-in migrations

Consider alternatives:

  • Pure APIs with async-first needs (FastAPI)
  • Minimal framework overhead (Flask)
  • Real-time heavy workloads without Channels

Project Structure

myproject/
├── manage.py
├── pyproject.toml
├── requirements/
│   ├── base.txt
│   ├── dev.txt
│   └── prod.txt
├── config/                    # Project configuration
│   ├── __init__.py
│   ├── settings/
│   │   ├── __init__.py
│   │   ├── base.py
│   │   ├── dev.py
│   │   └── prod.py
│   ├── urls.py
│   ├── wsgi.py
│   └── asgi.py
├── apps/                      # Django applications
│   ├── users/
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── views.py
│   │   ├── urls.py
│   │   ├── forms.py
│   │   ├── serializers.py     # If using DRF
│   │   ├── services.py        # Business logic
│   │   └── tests/
│   │       ├── __init__.py
│   │       ├── test_models.py
│   │       ├── test_views.py
│   │       └── test_services.py
│   └── core/                  # Shared utilities
│       ├── __init__.py
│       ├── models.py          # Abstract base models
│       └── mixins.py
├── templates/
│   ├── base.html
│   └── components/
├── static/
│   ├── css/
│   └── js/
└── tests/
    └── conftest.py
  • Split settings into base.py, dev.py, prod.py
  • Group apps under apps/ directory
  • Keep core/ app for shared abstract models and utilities
  • Place business logic in services.py, not in views
  • Co-locate tests inside each app under tests/ directory

Guardrails

Settings

  • Never hardcode SECRET_KEY -- use python-decouple or environment variables
  • Split settings: base.py (shared), dev.py (debug), prod.py (secure)
  • Always define AUTH_USER_MODEL before first migration
  • Set DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
  • Use ALLOWED_HOSTS in production (never ["*"])

Models

  • Always define __str__ on every model
  • Always set class Meta with db_table, ordering, and indexes
  • Use abstract base models for shared fields (TimeStampedModel, UUIDModel)
  • Use TextChoices/IntegerChoices for status fields (not raw strings)
  • Add related_name to all ForeignKey and OneToOneField relationships
  • Use on_delete explicitly: CASCADE, PROTECT, SET_NULL, SET_DEFAULT
  • Add database indexes for frequently queried fields
  • Use validators at model level for domain constraints

Abstract Base Models

# apps/core/models.py
from django.db import models
import uuid

class TimeStampedModel(models.Model):
    """Abstract base with created/updated timestamps."""
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

class UUIDModel(models.Model):
    """Abstract base with UUID primary key."""
    id = models.UUIDField(
        primary_key=True, default=uuid.uuid4, editable=False
    )

    class Meta:
        abstract = True

Custom User Model

  • Always create a custom user model before the first migration
  • Extend AbstractUser (not AbstractBaseUser unless you need full control)
  • Set AUTH_USER_MODEL = "users.User" in settings
# apps/users/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
from apps.core.models import TimeStampedModel, UUIDModel

class User(AbstractUser, UUIDModel, TimeStampedModel):
    email = models.EmailField(unique=True)
    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = ["username"]

    class Meta:
        db_table = "users"
        ordering = ["-created_at"]
        indexes = [models.Index(fields=["email"])]

    def __str__(self) -> str:
        return self.email

Views and URLs

Class-Based Views (CBVs)

  • Use ListView, DetailView, CreateView, UpdateView for CRUD
  • Use LoginRequiredMixin for authenticated views
  • Override get_queryset() to add filtering and select_related/prefetch_related
  • Override form_valid() to inject the current user
from django.views.generic import ListView
from django.db.models import Q
from .models import Product

class ProductListView(ListView):
    model = Product
    template_name = "products/list.html"
    context_object_name = "products"
    paginate_by = 20

    def get_queryset(self):
        qs = Product.objects.filter(
            status=Product.Status.PUBLISHED
        )
        search = self.request.GET.get("q")
        if search:
            qs = qs.filter(
                Q(name__icontains=search)
                | Q(description__icontains=search)
            )
        return qs.select_related("category")

URL Configuration

# config/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("admin/", admin.site.urls),
    path("api/", include("apps.api.urls")),
    path("", include("apps.products.urls")),
]
  • Use include() for app-level URL namespaces
  • Use app_name in each app's urls.py for reverse resolution
  • Serve media files in DEBUG mode only

ORM Essentials

Query Optimization

  • Always use select_related() for ForeignKey/OneToOne (SQL JOIN)
  • Always use prefetch_related() for ManyToMany/reverse FK (separate query)
  • Use only() or defer() to limit fields when not all columns needed
  • Never call Model.objects.all() without pagination or limits
  • Use F() expressions for database-level operations
  • Use Q() objects for complex lookups

Avoiding N+1 Queries

# BAD: N+1 queries
for product in Product.objects.all():
    print(product.category.name)  # Extra query per product

# GOOD: Single JOIN query
for product in Product.objects.select_related("category"):
    print(product.category.name)  # No extra queries

Transactions

  • Use @transaction.atomic for multi-step writes
  • Use select_for_update() for optimistic locking
from django.db import transaction

@transaction.atomic
def transfer_stock(source_id, dest_id, qty):
    source = Product.objects.select_for_update().get(id=source_id)
    dest = Product.objects.select_for_update().get(id=dest_id)
    source.stock -= qty
    dest.stock += qty
    source.save(update_fields=["stock"])
    dest.save(update_fields=["stock"])

Admin Configuration

from django.contrib import admin
from .models import Product

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ["name", "category", "price", "status"]
    list_filter = ["status", "category", "created_at"]
    search_fields = ["name", "description"]
    prepopulated_fields = {"slug": ("name",)}
    readonly_fields = ["created_at", "updated_at"]
    ordering = ["-created_at"]

    fieldsets = (
        (None, {"fields": ("name", "slug", "description")}),
        ("Pricing", {"fields": ("price", "stock")}),
        ("Classification", {"fields": ("category", "status")}),
        ("Timestamps", {
            "fields": ("created_at", "updated_at"),
            "classes": ("collapse",),
        }),
    )
  • Always register models with @admin.register(Model)
  • Use list_display for useful columns, list_filter for filtering
  • Use prepopulated_fields for slug generation
  • Group fields with fieldsets for organized admin forms

Django REST Framework (DRF)

Settings

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.SessionAuthentication",
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    "DEFAULT_PAGINATION_CLASS": (
        "rest_framework.pagination.PageNumberPagination"
    ),
    "PAGE_SIZE": 20,
    "DEFAULT_THROTTLE_RATES": {
        "anon": "100/hour",
        "user": "1000/hour",
    },
}

ViewSets and Routers

  • Use ModelViewSet for full CRUD
  • Use @action(detail=True/False) for custom endpoints
  • Override get_serializer_class() for different read/write serializers
  • Override get_queryset() to add select_related/prefetch_related
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response

class ProductViewSet(viewsets.ModelViewSet):
    queryset = Product.objects.select_related("category")
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get_serializer_class(self):
        if self.action in ["create", "update", "partial_update"]:
            return ProductCreateSerializer
        return ProductSerializer

    def perform_create(self, serializer):
        serializer.save(created_by=self.request.user)

    @action(detail=True, methods=["post"])
    def publish(self, request, pk=None):
        product = self.get_object()
        product.status = Product.Status.PUBLISHED
        product.save(update_fields=["status"])
        return Response({"status": "published"})

Serializers

  • Use ModelSerializer for standard CRUD
  • Add read_only_fields for computed/auto fields
  • Validate in validate_<field>() or validate() methods
  • Use nested serializers for read, flat IDs for write

Security Essentials

  • CSRF: Enabled by default for forms; use @csrf_exempt sparingly
  • XSS: Django templates auto-escape by default; never use |safe with user data
  • SQL Injection: ORM uses parameterized queries; never use raw SQL with string formatting
  • Clickjacking: X-Frame-Options middleware enabled by default
  • HTTPS: Set SECURE_SSL_REDIRECT = True in production
  • HSTS: Set SECURE_HSTS_SECONDS in production
  • Cookies: Set SESSION_COOKIE_SECURE = True and CSRF_COOKIE_SECURE = True
  • Passwords: Use AUTH_PASSWORD_VALIDATORS (enabled by default)
  • CORS: Use django-cors-headers with explicit allowed origins (never CORS_ALLOW_ALL_ORIGINS in production)

Testing

Standards

  • Use pytest with pytest-django (not Django's built-in test runner)
  • Mark database tests with @pytest.mark.django_db
  • Use factory-boy or fixtures for test data
  • Use APIClient for DRF endpoint testing
  • Coverage target: >80% for business logic
  • Test file naming: test_models.py, test_views.py, test_services.py

Fixtures

# conftest.py
import pytest
from rest_framework.test import APIClient
from apps.users.models import User

@pytest.fixture
def api_client():
    return APIClient()

@pytest.fixture
def user(db):
    return User.objects.create_user(
        username="testuser",
        email="test@example.com",
        password="testpass123",
    )

@pytest.fixture
def authenticated_client(api_client, user):
    api_client.force_authenticate(user=user)
    return api_client

Commands Reference

# Development
python manage.py runserver
python manage.py shell_plus          # django-extensions

# Migrations
python manage.py makemigrations
python manage.py migrate
python manage.py showmigrations

# Testing
pytest
pytest -v --cov=apps --cov-report=html
pytest apps/products/ -k "test_create"

# Database
python manage.py dbshell
python manage.py dumpdata products > fixtures/products.json
python manage.py loaddata fixtures/products.json

# Static files
python manage.py collectstatic

# Celery
celery -A config worker -l info
celery -A config beat -l info

Dependencies

Base: Django>=5.0, djangorestframework, django-cors-headers, django-filter, djangorestframework-simplejwt, python-decouple, psycopg2-binary, whitenoise

Dev: pytest, pytest-django, pytest-cov, factory-boy, django-debug-toolbar, django-extensions, black, ruff, mypy, django-stubs

Best Practices

Do

  • Use select_related and prefetch_related for every queryset
  • Create indexes for frequently queried fields
  • Use @transaction.atomic for multi-step operations
  • Validate at both model level and serializer level
  • Write services for business logic (not in views)
  • Use signals sparingly (prefer explicit service calls)
  • Cache expensive queries with Django cache framework
  • Use environment variables for all configuration

Don't

  • Put business logic in views
  • Use raw SQL without parameterization
  • Ignore N+1 query problems
  • Store sensitive data in settings files
  • Use Model.objects.all() without limits
  • Skip migrations in production
  • Use CORS_ALLOW_ALL_ORIGINS = True in production
  • Use |safe template filter with user-provided data

Advanced Topics

For detailed code examples and advanced patterns, see:

  • references/patterns.md -- DRF serializers, middleware, services, signals, Celery tasks, management commands, deployment, and testing patterns

External References

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

39.19%
按下载量换算39

Claude

29.29%
按下载量换算29

Cursor

18.46%
按下载量换算18

Gemini CLI

9.36%
按下载量换算9

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

敏感数据

该 Skill 可能接触密钥、Token、环境变量或敏感配置,应进入高风险复核队列,默认不自动发布。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills