当get请求传参时,用?分隔参数和域名,用&分隔参数,如果参数里面本身就有&符号就会识别不出来,还是会当成分隔符,所以这些数据在传输的时候,就需要转义,现在普遍是转成urlencode编码:%20%xx%23

在jinja2模板里面,可以使用 data|urlencode 发送urlencode编码,而python里面又有urllib.parse.unquote()可以解析urlencode编码

视图函数

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

html:访问"/"返回html,在html上面点击超链接时请求"/rq/",get_request函数获取数据,并解析

由于此时没有用urlencode编码,所以&会被视为分隔符,即只能获取到name1

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

path1

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

path2

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

path3

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

加上urlencode编码

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

测开之路一百二十五:flask之urlencode参数传递和解析-LMLPHP

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>
05-16 08:52