[英]Get and Post methods in Python (Flask)
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. 我是Flask和Web开发的新手,我正在尝试创建一个简单的应用程序,在服务器上生成一个整数数组并发送给客户端。 Here is some sample (working) code in app.py:
以下是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? 即使没有GET / POST HTTP方法,这如何工作? 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 ).
我认为服务器和客户端之间的通信需要获取/发布http方法(如下所述: 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? 有没有办法重写这个,以便它使用POST? Apparently, POST is better for dealing with sensitive data, as described here: http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
显然,POST更适合处理敏感数据,如下所述: http : //blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post
Thanks. 谢谢。
The default for flask is GET
. 烧瓶的默认值是
GET
。 You can use methods
to change that: 您可以使用
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.
Web应用程序在访问URL时使用不同的HTTP方法。 You should familiarize yourself with the HTTP methods as you work with Flask.
在使用Flask时,您应该熟悉HTTP方法。 By default, a route only answers to GET requests.
默认情况下,路由仅响应GET请求。 You can use the methods argument of the route() decorator to handle different HTTP methods.
您可以使用route()装饰器的methods参数来处理不同的HTTP方法。
The default is GET
. 默认为
GET
。 POST
is applicable only if abc.html
has a form and the user submits the value of s_abc
. 仅当
abc.html
具有表单且用户提交s_abc
的值时, POST
才适用。 In your case, you're generating it and rendering the html out. 在你的情况下,你正在生成它并渲染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:
它将向您展示如何创建表单和接收数据:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.