Web Frameworks
Build web apps with Flask and Django—routing, views, templates, and ORM integration.
Flask Basics
Flask is a microframework: create app, register routes with decorators, return strings or jsonify dicts. Extensions add SQLAlchemy, migrations, and auth.
Request context provides request and session proxies. Blueprints modularize large apps.
Flask suits APIs and small services where you choose components explicitly.
- Use application factories for testing multiple configs
- Never run debug=True in production
- Validate input with pydantic or marshmallow schemas
from flask import Flask, jsonify
app = Flask(__name__)
@app.get("/api/health")
def health():
return jsonify(ok=True)Django Framework
Django is batteries-included: ORM, admin, auth, forms, and migrations. Projects contain apps; settings.py configures databases and middleware.
Class-based views and Django REST Framework accelerate API development. Migrations evolve schemas with manage.py migrate.
Choose Django when admin panels, auth, and structured MVC conventions speed delivery.
- Keep business logic in services, views thin
- Use select_related and prefetch_related to avoid N+1 queries
- Configure ALLOWED_HOSTS and SECRET_KEY per environment