How Python web frameworks work
Python powers the backends of Instagram, Spotify, Dropbox, Reddit and countless startups. Its two most popular web frameworks take different approaches: Django, "the web framework for perfectionists with deadlines", ships everything included; FastAPI is a modern, fast framework focused on APIs with automatic validation and documentation. This track teaches both.
The editors in this track run real Python 3.14, FastAPI and Django in your browser. The first run of a lesson downloads the framework (a few MB, then cached). Instead of starting a server and opening a browser, each example sends requests to the app in code, the same way automated tests do.
What a web framework does
Every web app does the same basic job: receive an HTTP request, decide which code handles it (routing), run that code, and send back a response. A framework handles the repetitive parts:
| Job | Without a framework | With one |
|---|---|---|
| Parse URLs, headers, query strings, bodies | By hand | request.query_params, request.json() |
| Routing | if path == … chains |
@app.get("/items/{id}") or path("items/<int:id>/", …) |
| Validation | Manual checks | Type hints (FastAPI) or forms (Django) |
| Database | Raw SQL | An ORM (Django ORM, SQLAlchemy, SQLModel) |
| Security | Easy to forget | CSRF protection, password hashing, escaping built in |
The interface underneath: WSGI and ASGI
Python web servers and frameworks agree on a standard interface, so any server can run any framework.
WSGI (Web Server Gateway Interface, 2003) is a function that receives the request as a dictionary and returns the body. Django and Flask are WSGI frameworks. Here's a complete WSGI app, called directly (no server needed):
ASGI (Asynchronous Server Gateway Interface) is the modern, async successor: an async function that receives and sends messages. It supports WebSockets and long-lived connections, and lets one process handle many requests while they wait on databases or other APIs. FastAPI and Starlette are ASGI frameworks; Django supports both.
You'll never write raw WSGI or ASGI in practice, but now you know what gunicorn myproject.wsgi and uvicorn main:app actually run.
Django, FastAPI or Flask?
| Django | FastAPI | Flask | |
|---|---|---|---|
| Style | "Batteries included" | API-first, type-hint driven | Minimal, add what you need |
| Interface | WSGI and ASGI | ASGI | WSGI |
| Database | Built-in ORM + migrations | Your choice (SQLModel, SQLAlchemy) | Your choice |
| Admin panel | ✔ automatic | ✖ | ✖ |
| HTML pages | ✔ templates, forms | Possible (Jinja2) | ✔ Jinja2 |
| Validation | Forms, serializers (DRF) | Pydantic, from type hints | Extensions |
| API docs | With DRF + extensions | ✔ automatic (OpenAPI, Swagger UI) | Extensions |
| Best for | Full web apps, content sites, admin-heavy products, startups that must move fast | APIs, microservices, ML model serving, async workloads | Small apps and prototypes |
Both main frameworks are excellent; many companies use Django for the main product and FastAPI for specific services. The skills transfer: HTTP, routing, ORMs, validation and authentication work the same way everywhere.
Setting up a project on your computer
Always use a virtual environment per project (see the Python track's last lesson):
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install "fastapi[standard]" # FastAPI + uvicorn + extras
pip install django # or DjangoOr with uv, the fast modern tool: uv init shop && cd shop && uv add "fastapi[standard]".
Practice
- Add a
/timeroute to the WSGI app that returns a fixed time string. - Change the ASGI app to return a
404status for any path that doesn't start with/products. - Decide which framework you'd pick for (a) a news site with editors, (b) an API that serves a machine-learning model, and explain why.
1.What is ASGI?
2.Which framework includes an ORM, migrations and an admin panel out of the box?
3.What does FastAPI use to validate request data?