我搜索并阅读了许多类似的帖子,但是我的问题的行为与发现的行为不同。
我有一个超级简单的烧瓶应用程序(如下)。所做的全部工作就是在test.html页面上显示两个按钮,以回调到python def。
可能是什么问题? 转义字符是问题吗?如果是这样,我该如何预防?
test.py
#!/usr/bin/env python3
from flask import Flask, request, send_file
app = Flask(__name__, static_url_path='', static_folder='')
@app.route('/')
def FooTest():
return app.send_static_file('test.html')
@app.route("/test", methods=["GET","POST"])
def test():
print("Got Callback:")
return "Click"
if __name__ == '__main__':
app.run(debug=True)
test.html
<!DOCTYPE html>
<html>
<form method="post" action="/test">
<button type="submit"> Test1</button/>
</form>
<form method="post" action="{{ url_for('test') }}">
<button type="submit"> Test2</button/>
</form>
</html>
答案 0 :(得分:1)
您的test.html是Jinja2模板,可以通过调用render_template
来创建所需的HTML进行处理。您按原样返回模板,因此浏览器尝试调用不存在的http://localhost:5000/{{ url_for('test') }}
。像这样更改FooTest
(您可能需要调整文件路径):
from flask import render_template
@app.route('/')
def FooTest():
return render_template('test.html')