---
name: flask-73764480f0e7
description: Avoid common Flask mistakes — context errors, circular imports, session configuration, and production gotchas.
metadata: {"clawdbot":{"emoji":"🍶","requires":{"bins":["python3"]},"os":["linux","darwin","win32"]}}
---

## Application Context
- `current_app` only works inside request or with `app.app_context()` — "working outside application context" error
- `g` is per-request storage — lost after request ends, use for db connections
- Background tasks need context — `with app.app_context():` or pass data, not proxies
- `create_app()` factory pattern avoids circular imports — import `current_app` not `app`

## Request Context
- `request`, `session` only inside request — "working outside request context" error
- `url_for` needs context — `url_for('static', filename='x', _external=True)` for absolute URLs
- Test client provides context automatically — but manual context for non-request code

## Circular Imports
- `from app import app` in models causes circular — use factory pattern
- Import inside function for late binding — or use `current_app`
- Blueprints help organize — register at factory time, not import time
- Extensions init with `init_app(app)` pattern — create without app, bind later

## Sessions and Security
- `SECRET_KEY` required for sessions — random bytes, not weak string
- No SECRET_KEY = unsigned cookies — anyone can forge session data
- `SESSION_COOKIE_SECURE=True` in production — only send over HTTPS
- `SESSION_COOKIE_HTTPONLY=True` — JavaScript can't access

## Debug Mode
- `debug=True` in production = remote code execution — attacker can run Python
- Use `FLASK_DEBUG` env var — not hardcoded
- Debug PIN in logs if debug enabled — extra layer, but still dangerous

## Blueprints
- `url_prefix` set at registration — `app.register_blueprint(bp, url_prefix='/api')`
- Blueprint routes relative to prefix — `@bp.route('/users')` becomes `/api/users`
- `blueprint.before_request` only for that blueprint — `app.before_request` for all

## SQLAlchemy Integration
- `db.session.commit()` explicitly — autocommit not default
- Session scoped to request by Flask-SQLAlchemy — but background tasks need own session
- Detached object error — object from different session, refetch or merge
- `db.session.rollback()` on error — or session stays in bad state

## Production
- `flask run` is dev server — use Gunicorn/uWSGI in production
- `threaded=True` for dev server concurrency — but still not production-ready
- Static files through nginx — Flask serving static is slow
- `PROPAGATE_EXCEPTIONS=True` for proper error handling with Sentry etc.

## Common Mistakes
- `return redirect('/login')` vs `return redirect(url_for('login'))` — url_for is refactor-safe
- JSON response: `return jsonify(data)` — not `return json.dumps(data)`
- Form data in `request.form` — JSON body in `request.json` or `request.get_json()`
- `request.args` for query params — `request.args.get('page', default=1, type=int)`


---
Source attribution: modbender
Original project: https://github.com/modbender/skill-library-mcp/blob/d9c8fe903b5b4e82237dbe8d0df8567783022e18/data/flask/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2025 skill-library-mcp contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: modbender
Original project: https://github.com/modbender/skill-library-mcp/blob/d9c8fe903b5b4e82237dbe8d0df8567783022e18/data/flask/SKILL.md
Attribution basis: Source repository record

Fields of work: Development & Engineering
Roles: Backend Developer, DevOps / SRE, Full-Stack Developer
Specialties: Backend
Tools: Flask, Sqlalchemy
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 430e1611528bc3f34ebdd4cf0c7ae503a92837ade402aa6d5ac674a4eba1bdd1
