Measuring open the road to one hundred twenty-five: flask of parameter passing and parsing urlencode

 

 

When the get request parameter passing with? Separate parameters and the domain name, separated by & parameters, if the argument itself is inside the ampersand will not identify them, or will it as a delimiter, so these data at the time of transmission, we need to turn Yi, is now generally turn into urlencode coding:% 20% xx% 23

In jinja2 template inside, you can use the data | urlencode urlencode send coded, and python inside there urllib.parse.unquote () can resolve urlencode coding

 

View function

 

html: access to "/" request to return html, click on the hyperlink above html "/ rq /", get_request function gets the data and analytic

Since no use urlencode coding, it would be considered & separators, i.e. only acquired name1

 

 

 Pthl

 

path2

path3

 

 

Plus urlencode coding

 

 

 

 

 

from flask import Flask, render_template
from flask import request
from urllib.parse import unquote

app = Flask(__name__)


@app.route("/")
def index():
return render_template("index.html")


@app.route("/rq/")
def get_request():
""" request数据 """
name = request.args.get('name', "没有获取到name") # 获取指定参数
return unquote(name) # 解码


if __name__ == '__main__':
app.run(debug=True)


{% set name= "name=name1&name2#name3"%}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>地址栏编码</title>
</head>
<body>
<a href="/rq/">跳转到path1</a>
<a href="/rq/?name=name1">跳转到path2</a>
<a href="/rq/?name=name1&name2#name3">跳转到path3</a>
<a href="/rq/?name={{ name|urlencode }}">跳转到path4</a>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/zhongyehai/p/11442824.html