16
Web + frameworks / Intermediate
Flask and Django frameworks
How Flask and Django organize Python web applications, where each fits, and the production concerns both frameworks share.
01Choosing a framework
| Area | Flask | Django |
|---|---|---|
| Style | Small core / choose components | Batteries included |
| ORM | Usually SQLAlchemy | Built-in Django ORM |
| Admin | Build/customize | Built-in admin |
| Best fit | Focused services, custom architecture | Content/business apps needing integrated conventions |
02Flask application factory
pythonFactory pattern
from flask import Flask
def create_app():
app = Flask(__name__)
app.config.from_mapping(SECRET_KEY='dev-only')
from .public import bp
app.register_blueprint(bp)
return app
03Django model + route idea
pythonDjango model
class Project(models.Model):
slug = models.SlugField(unique=True)
name = models.CharField(max_length=180)
published = models.BooleanField(default=True)
04Shared production concerns
- Strong secret management.
- Database migrations.
- Secure cookies and CSRF protection.
- Authentication/authorization.
- Reverse-proxy awareness.
- Static/media strategy.
- Tests, dependency audit, logs, health checks, backups, and rollback plans.