我是 Flask 和网络开发的新手,我正在尝试创建一个简单的应用程序,其中在服务器上生成一个整数数组并将其发送到客户端。这是 app.py 中的一些示例(工作)代码:
from flask import Flask, render_template, request, url_for
import random
app = Flask(__name__)
@app.route('/')
def form():
s_abc = [random.random() for _ in range(40)]
return render_template('abc.html', s_abc=s_abc)
if __name__ == '__main__':
app.run(debug=True)
这里是 abc.html 的(有效)片段:
<div>
{{s_abc}}
</div>
我的问题是:
即使没有 GET/POST HTTP 方法,它是如何工作的?我认为服务器和客户端之间的通信需要 get/post http 方法(如此处所述:http://www.tutorialspoint.com/http/http_methods.htm)。然而,即使我没有写这样的代码,我的代码仍然有效:
@app.route('/', methods=['GET'])
有没有办法重写它以便它使用 POST?显然,POST 更适合处理敏感数据,如下所述:http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
谢谢。
最佳答案
flask 的默认值是 GET
。您可以使用方法
来改变它:
@app.route('/', methods=['GET', 'POST'])
阅读文档:Flask 1.0.2 documentation
Web applications use different HTTP methods when accessing URLs. You should familiarize yourself with the HTTP methods as you work with Flask. By default, a route only answers to GET requests. You can use the methods argument of the route() decorator to handle different HTTP methods.
关于python - Python (Flask) 中的 Get 和 Post 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31081178/