← 返回
开发者工具 中文

Flask

Avoid common Flask mistakes — context errors, circular imports, session configuration, and production gotchas.
避免常见的Flask错误——上下文错误、循环导入、会话配置以及生产环境陷阱。
ivangdavila ivangdavila 来源
开发者工具 clawhub v1.0.0 1 版本 99883.9 Key: 无需
★ 2
Stars
📥 1,681
下载
💾 33
安装
1
版本
#latest

概述

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)

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-28 23:59 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 72 📥 181,412
dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 677 📥 325,698
ai-agent

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,383 📥 320,794