web应用程序中未更新MySQL数据-Python/Flask
我正在使用MySQL后端构建Python/Flask应用程序。许多视图都有表单将数据提交到路由,然后路由通过web应用程序中未更新MySQL数据-Python/Flask,python,mysql,flask,sqlalchemy,apache2,Python,Mysql,Flask,Sqlalchemy,Apache2,我正在使用MySQL后端构建Python/Flask应用程序。许多视图都有表单将数据提交到路由,然后路由通过db.session.commit()添加数据。我可以确认MySQL db在每次提交时都会更新,但是如果我随后多次刷新页面,则每次数据都会更改(即最近添加的项目将消失、重新出现) 我已经做了一些事情来尝试修复: HTML标题 我知道添加大量的头并不是最好的,我希望是最好的,但我添加这些头是为了对抗缓存: <meta http-equiv="cache-control" content
db.session.commit()
添加数据。我可以确认MySQL db在每次提交时都会更新,但是如果我随后多次刷新页面,则每次数据都会更改(即最近添加的项目将消失、重新出现)
我已经做了一些事情来尝试修复:
HTML标题我知道添加大量的头并不是最好的,我希望是最好的,但我添加这些头是为了对抗缓存:
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="private, proxy-revalidate, s-maxage=0, no-cache, no-store, must-revalidate" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
禁用MySQL缓存
Apache2禁用缓存
您可以尝试关闭所有您所做的特殊更改,这显然是假设某些缓存机制负责观察到的结果,并显示您的flask站点的一些代码。然后开始收集实际发生的事情的文档证据。我实际上已经弄明白了这一点-解决方法是在查询之前添加
db.session.commit()
。不确定这是否是一种“正确”的方法,但它确实适用于我的小规模应用程序 谢谢!我继续前进,把所有东西都禁用了。代码库在上可用,但作为示例,我可以深入了解特定路线-将编辑我的初始问题。相关:
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
response.headers["Pragma"] = "no-cache" # HTTP 1.0.
response.headers["Expires"] = "0" # Proxies.
SET SESSION query_cache_type=OFF;
<filesMatch "\.(html|htm|js|css)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</filesMatch>
@app.route('/projects', methods=['GET','POST'])
@login_required
def projects():
user = return_current_user()
if request.method == 'POST':
if request.form['title'] and request.form['description']:
project = Projects(request.form['title'], request.form['description'])
if project.title != None and project.description != None:
user.projects.append(project)
db.session.commit()
return redirect('/projects')
elif request.method == 'GET':
if 'clear-page' in request.args:
return redirect('/projects')
elif 'query' in request.args:
projects = Projects.query.filter(Projects.title.like('%'+request.args['query']+'%')).order_by(Projects.timestamp.desc()).all()
else:
projects = Projects.query.order_by(Projects.timestamp.desc()).all()
return render_template('projects.html', title="Projects", user=user, projects=projects)