【发布时间】:2017-12-09 13:42:00
【问题描述】:
当我使用 python test.py 运行烧瓶,然后在浏览器中导航到 http://localhost:5000 时,我希望终端能够读取:
test-1 test-2 test-3
这三个打印语句仅在第一次运行test.py 时出现。之后,终端只会显示:
test-1 test-2
由于某种原因,在初始运行后没有重定向到第二页。是否发生了某种奇怪的缓存废话?有人可以解释发生了什么吗?
from flask import Flask, redirect, url_for, send_from_directory
app = Flask(__name__)
@app.route('/')
def home():
print('test-2')
return redirect(url_for('page2'))
@app.route('/secondPage')
def page2():
print('test-3')
return send_from_directory('.', 'test_dashboard_4.html')
if __name__ == '__main__':
print('test-1')
app.run(debug=True)
【问题讨论】:
-
你为什么没有
app.run()? -
所以这段代码显然不会运行。请发布一个工作示例。
-
好收获。我添加了
app.run()。sys.exit()只是为了终止程序,也解决了这个问题。 -
不确定添加 app.run 后编号是否更改。当我运行它时,我看到了 test-1、test-2、test-3。然后按预期进行 test-2、test-3。
-
@bren,你是否在两次运行之间重新启动了烧瓶脚本?
标签: python redirect flask url-for