Python (Flask) 中的 Get 和 Post 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31081178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get and Post methods in Python (Flask)
提问by codersun3
I'm new to Flask and web development, and I am trying to create a simple app where an array of integers is generated on the server and sent to the client. Here is some sample (working) code in app.py:
我是 Flask 和 Web 开发的新手,我正在尝试创建一个简单的应用程序,其中在服务器上生成整数数组并将其发送到客户端。这是 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)
And here is a (working) snippet of abc.html:
这是 abc.html 的(工作)片段:
<div>
{{s_abc}}
</div>
My questions are:
我的问题是:
How does this work even though there are no GET/POST HTTP methods? I thought that get/post http methods were required for communication between the server and the client (as described here: http://www.tutorialspoint.com/http/http_methods.htm). However, my code works even though I did not write something like this:
@app.route('/', methods=['GET'])
Is there a way to rewrite this so that it uses POST? Apparently, POST is better for dealing with sensitive data, as described here: http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
即使没有 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
Thanks.
谢谢。
采纳答案by Subbdue
The default is GET
. POST
is applicable only if abc.html
has a form and the user submits the value of s_abc
. In your case, you're generating it and rendering the html out.
默认值为GET
. POST
仅当abc.html
具有表单并且用户提交 的值时才适用s_abc
。在您的情况下,您正在生成它并呈现 html。
If you're new to flask, you should check out this complete tutorial. It will show you how to create forms and receive data:
如果您不熟悉 Flask,您应该查看这个完整的教程。它将向您展示如何创建表单和接收数据:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms
回答by Reut Sharabani
The default for flask is GET
. You can use methods
to change that:
烧瓶的默认值是GET
. 你可以用它methods
来改变:
@app.route('/', methods=['GET', 'POST'])
Read the docs: Flask 1.0.2 documentation
阅读文档:Flask 1.0.2 文档
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.
Web 应用程序在访问 URL 时使用不同的 HTTP 方法。在使用 Flask 时,您应该熟悉 HTTP 方法。默认情况下,路由只响应 GET 请求。您可以使用 route() 装饰器的 methods 参数来处理不同的 HTTP 方法。