# 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. ## Install Save in your project root: ## Quick Use ```bash 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: ```bash python manage.py startapp api ``` ```python # 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})" ``` ```python # 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}) ``` ## Intro 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. - **Repo**: https://github.com/django/django - **Stars**: 87K+ - **Language**: Python - **License**: BSD 3-Clause ## 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 ```bash # 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 - Docs: https://docs.djangoproject.com - GitHub: https://github.com/django/django - License: BSD 3-Clause --- Source: https://tokrepo.com/en/workflows/cc164535-362e-11f1-9bc6-00163e2b0d79 Author: AI Open Source