使用python flask开发了一个Web应用程序,它具有以下功能,
在客户端,用户可以查看输入表单,一旦用户单击生成按钮,随后应用程序执行任务,输出文本文件可以发送到客户端。这里的问题,在客户端看起来像一些缓存发生,好像服务器端的输出文件被更改,更改的客户端将不会反映文件的早期内容仍显示给用户。
我的代码段如下,
53 #View the results captures.
54 @app.route("/view")
55 def view():
56 return render_template("view.html")
57
58 #Download file.
59 @app.route('/static/<path:filename>/',methods=['GET','POST'])
60 def download_file (filename):
61 try:
62 return send_from_directory
(directory=static,filename=filename)
63
64 except Exception as e:
65 return str(e)
view.html
1 <!doctype html>
2 <html>
3 <body>
4 {% with messages = get_flashed_messages() %}
5 {% if messages %}
6 <ul>
7 {% for message in messages %}
8 <li>{{ message }}</li>
9 {% endfor %}
10 </ul>
11 {% endif %}
12 {% endwith %}
13
14 <h1>Your result is ready.</h1>
15 <a href = "/static/bt_output.txt" target="_blank"><button
class='btn btn-default'>View Result!</button></a>
16
17 <!-- <p>Do you want to <a href = "{{ url_for('main') }}">
18 <b>View Result</b></a></p>-->
19
20
21 </body>
22 </html>
23
答案 0 :(得分:0)
为Jinja添加时间戳功能(例如在你的init.py中):
def get_timestamp():
return int(time.time())
app.jinja_env.globals['timestamp'] = get_timestamp
并将您的HTML更改为:
<a href="/static/bt_output.txt?time={{ timestamp() }}" target="_blank">
<button class='btn btn-default'>View Result!</button>
</a>
您可以使用response.cache_control对任何缓存控制标头进行特定于文件/路由的更改,否则您应该查看服务器设置以获取默认值。
@app.route('/static/<path:filename>/',methods=['GET','POST'])
def download_file (filename):
try:
response = send_from_directory(directory=static,
filename=filename)
response.cache_control.max_age = 60 # e.g. 1 minute
return response
except Exception as e:
return str(e)
您还应该查看SEND_FILE_MAX_AGE_DEFAULT
configuration setting