【发布时间】:2020-10-09 22:00:18
【问题描述】:
我正在使用 Flask 构建一个 Web 表单,并希望用户能够输入多个条目,并让他们有机会在将数据发送到数据库之前使用撤消按钮对条目感到遗憾。我正在尝试使用Flask-Caching,但未能正确设置。
我已关注The Flask Mega-Tutorial 设置 Flask(这是我的第一个 Flask 应用程序)。
+---app
| | forms.py
| | routes.py
| | __init__.py
| +---static
| +---templates
我想知道我需要如何配置 Flask 应用程序才能基本上能够执行以下操作:
cache.add("variable_name", variable_data)
variable_name = cache.get("variable_name")
cache.clear()
在其中一个页面中(带有@app.route 装饰器的函数)?
在 app.init.py 我有:
from flask import Flask
from config import Config
from flask_caching import Cache
app = Flask(__name__)
app.config.from_object(Config)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
from app import routes
在 routes.py 我有:
from flask import current_app
当我尝试调用缓存时,我使用下面的代码。
current_app.cache.add("variable_name", variable_data)
我在尝试使用表单时得到以下错误:
AttributeError: 'Flask' object has no attribute 'cache'
我发现的几乎所有教程都只是在同一个模块中包含了应用声明和所有路由。但是当我在另一个模块中有路由时如何访问缓存?
【问题讨论】:
标签: python flask caching flask-caching