Flask 简明教程
Flask – HTTP methods
Http 协议是万维网中数据通信的基础。此协议中定义了不同方法才能从指定 URL 检索数据。
Http protocol is the foundation of data communication in world wide web. Different methods of data retrieval from specified URL are defined in this protocol.
下表总结了不同的 http 方法 −
The following table summarizes different http methods −
Sr.No. |
Methods & Description |
1 |
GET Sends data in unencrypted form to the server. Most common method. |
2 |
HEAD Same as GET, but without response body |
3 |
POST Used to send HTML form data to server. Data received by POST method is not cached by server. |
4 |
PUT Replaces all current representations of the target resource with the uploaded content. |
5 |
DELETE Removes all current representations of the target resource given by a URL |
默认情况下,Flask 路由响应 GET 请求。然而,可以通过向 route() 装饰器提供 methods 参数来更改此首选项。
By default, the Flask route responds to the GET requests. However, this preference can be altered by providing methods argument to route() decorator.
为了说明如何在 URL 路由中使用 POST 方法,首先让我们创建一个 HTML 表单,并使用 POST 方法将表单数据发送至 URL。
In order to demonstrate the use of POST method in URL routing, first let us create an HTML form and use the POST method to send form data to a URL.
将以下脚本保存为 login.html
Save the following script as login.html
<html>
<body>
<form action = "http://localhost:5000/login" method = "post">
<p>Enter Name:</p>
<p><input type = "text" name = "nm" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
现在在 Python shell 中输入以下脚本。
Now enter the following script in Python shell.
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
@app.route('/success/<name>')
def success(name):
return 'welcome %s' % name
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'POST':
user = request.form['nm']
return redirect(url_for('success',name = user))
else:
user = request.args.get('nm')
return redirect(url_for('success',name = user))
if __name__ == '__main__':
app.run(debug = True)
在开发服务器开始运行后,在浏览器中打开 login.html ,在文本字段中输入 name,然后单击 Submit 。
After the development server starts running, open login.html in the browser, enter name in the text field and click Submit.
表单数据通过 form 标记的动作子句 POST 至 URL。
Form data is POSTed to the URL in action clause of form tag.
http://localhost/login 映射至 login() 函数。由于服务器通过 POST 方法接收了数据,因此可通过以下方式获取从表单数据中获取的“nm”参数值:
http://localhost/login is mapped to the login() function. Since the server has received data by POST method, value of ‘nm’ parameter obtained from the form data is obtained by −
user = request.form['nm']
它作为变量部分传递至 ‘/success’ URL。浏览器在窗口中显示 welcome 消息。
It is passed to ‘/success’ URL as variable part. The browser displays a welcome message in the window.
在 login.html 中将方法参数更改为 ‘GET’ ,并在浏览器中再次将其打开。服务器上收到的数据通过 GET 方法。现在可通过以下方式获取“nm”参数值:
Change the method parameter to ‘GET’ in login.html and open it again in the browser. The data received on server is by the GET method. The value of ‘nm’ parameter is now obtained by −
User = request.args.get(‘nm’)
此处, args 是包含表单参数及其对应值的列表对的字典对象。与之前一样,与“nm”参数对应的值传递给“/success”URL。
Here, args is dictionary object containing a list of pairs of form parameter and its corresponding value. The value corresponding to ‘nm’ parameter is passed on to ‘/success’ URL as before.