Quick Reference · Python WSGI micro-framework

flask cheat sheet

Every request travels through the same loop: the app object routes a URL to a view function, which reads the request, talks to templates / a database as needed, and hands back a response. Learn that loop once and the decorators stop being a list to memorize.

app & config routing request / response templates / static models / database sessions / auth production note most common

Distilled & cross-checked across: github.com/lucrae/flask-cheat-sheet · cheatsheets.zip/flask · devsheets.io/sheets/flask · prettyprinted flask cheatsheet (PDF) · codecademy.com/learn/introduction-to-flask

The request/response loop & where each piece of Flask plugs in
Client browser / curl / fetch GET /user/3 Router @app.route(...) url_map matches rule View Function reads request, decides request.args / .json Template render_template() Jinja2 + static/ Database SQLAlchemy model db.session.query() Response jsonify / HTML status + headers return value → Flask wraps it as an HTTP Response OUTSIDE YOUR APP
01Install & Runonce per project
02App Objectthe entry point
03Configurationenv-aware settings
04Routing & MethodsURL → function
05Dynamic RoutesURL converters
06The Request Objectincoming data
07Building Responseswhat goes back out
08Jinja2 Templatesrender_template
09Static Filescss / js / images
10Sessions & Cookiesper-user state
11Middleware & Errorshooks around requests
12Blueprintsstructuring bigger apps
13Models · SQLAlchemyflask-sqlalchemy
14Migrationsflask-migrate
15Login & Shellflask-login · flask shell
Common Config Keysapp.config[...]

Project structure

Two shapes of the same idea — flat for a single-file app, package + blueprints once it grows.

Single file

Everything in one module — fine for prototypes and the smallest apps.

project/
├── app.py           # Flask(), routes, app.run()
├── templates/
│   └── index.html
├── static/
│   └── style.css
└── config.py

App factory + Blueprints

Routes split by feature; __init__.py wires blueprints into one app object.

run.py
project/
  ├── __init__.py   # create_app(), register_blueprint()
  ├── config.py
  ├── models.py
  ├── admin/
  │   └── routes.py # admin = Blueprint(...)
  ├── main/
  │   └── routes.py # main = Blueprint(...)
  ├── templates/
  └── static/css/

Template inheritance

A base layout defines the shell once; child pages only fill in the blocks that differ.

base.html {% block content %}{% endblock %} index.html extends + fills block about.html extends + fills block

Worth memorizing

app.run() ≠ flask runCLI needs FLASK_APP set; both start the dev server
request.form vs .argsform = POST body; args = query string
jsonify ≠ json.dumpsjsonify sets the correct mimetype + headers too
render_template ≠ return htmltemplates live in templates/, support Jinja2 logic
db.session.addstages a row — commit() actually writes it
Blueprint ≠ sub-appit's a deferred set of routes, registered onto one real app
SECRET_KEYrequired for sessions, flash messages, CSRF tokens
flask db migrate → upgradefirst generates a script, second actually applies it