Configs2026年4月12日·1 分钟阅读

Django — The Web Framework for Perfectionists with Deadlines

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Batteries-included: ORM, admin, auth, migrations, forms, caching, and security. Powers Instagram, Spotify, Mozilla, Disqus, and NASA.

AI
AI Open Source · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

pip install django
django-admin startproject mysite
cd mysite
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
# Visit http://localhost:8000
# Admin at http://localhost:8000/admin

Create an app:

python manage.py startapp api
# api/models.py
from django.db import models

class Asset(models.Model):
    repo = models.CharField(max_length=255, unique=True)
    stars = models.IntegerField(default=0)
    description = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return f"{self.repo} ({self.stars})"
# api/views.py
from django.http import JsonResponse
from .models import Asset

def top_assets(request):
    assets = Asset.objects.order_by("-stars")[:10]
    data = [{"repo": a.repo, "stars": a.stars} for a in assets]
    return JsonResponse({"assets": data})
介绍

Django is a high-level Python web framework created by Adrian Holovaty and Simon Willison at the Lawrence Journal-World newspaper in 2003, open-sourced in 2005. Its motto: The web framework for perfectionists with deadlines. Django includes everything you need out of the box: ORM, admin panel, authentication, URL routing, templating, form handling, file uploads, caching, and security middleware.

What Django Does

  • ORM — define models in Python, auto-generate SQL
  • Admin — auto-generated CRUD admin panel
  • Auth — users, groups, permissions, sessions, CSRF
  • Migrations — schema migrations from model changes
  • URL routing — regex and path-based URL dispatcher
  • Templates — Django template language (DTL) or Jinja2
  • Forms — validation, rendering, model forms
  • Middleware — request/response processing pipeline
  • Caching — Redis, Memcached, file, DB backends
  • Security — XSS, CSRF, SQL injection, clickjacking protections
  • Django REST Framework — add-on for building APIs

Architecture

MVT (Model-View-Template): Model is the ORM layer, View handles logic and returns responses, Template renders HTML. WSGI/ASGI compatible — deploy behind Gunicorn, uvicorn, or Daphne. Settings-based configuration via settings.py.

Self-Hosting

# Production
pip install gunicorn
gunicorn mysite.wsgi:application --bind 0.0.0.0:8000 --workers 4

# Docker
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "mysite.wsgi", "--bind", "0.0.0.0:8000"]

Key Features

  • Batteries-included (ORM, admin, auth, migrations)
  • Auto-generated admin panel
  • Django REST Framework for APIs
  • ASGI support (async views)
  • Comprehensive security defaults
  • Mature ecosystem (10,000+ packages)
  • Excellent documentation
  • Huge community

Comparison

Framework Language Philosophy Best For
Django Python Batteries-included Full apps
FastAPI Python Async + types APIs
Flask Python Micro Small APIs
Rails Ruby Convention > config Full apps
Laravel PHP Elegant syntax Full apps
Spring Boot Java Enterprise Enterprise

常见问题 FAQ

Q: Django vs FastAPI? A: Django 是全栈框架(ORM、admin、auth 全包),适合传统 web app + API;FastAPI 专注高性能异步 API,适合纯 API 后端和微服务。

Q: Django 支持 async 吗? A: 支持。Django 4.1+ 支持 async views、middleware、ORM(部分)。完整 async ORM 仍在推进中。

Q: admin 可以生产用吗? A: 内部管理可以。面向用户的后台建议基于 Django admin 定制或用 Django Unfold/Grappelli 等主题。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产