1,get请求。
使用场景:获取信息并没有对服务器的数据或者资源进行修改,则用get。
传参:get请求传参是放在URL中,通过“?”的形式指定键值对。
2,post请求。
使用场景:对服务器产生影响,则用post。
传参:post不是放在URL中,而是“formdata”的形式发送给服务器。
3,使用:
@app.route('/')def hello_world():return render_template('index.html')@app.route('/search/')def search():return 'search!'对应的index页面为:<a href="{{ url_for("search",q = "hello") }}">搜寻关键字为hello</a>
4,获取get请求中的关键字。
@app.route('/')def hello_world():return render_template('index.html')# 获取用户提交的关键字@app.route('/search/')def search():haha = request.args.get('q')print(haha)return '用户提交的关键字是:%s' %haha
<a href="{{ url_for("search",q = "hello") }}">搜寻</a>
5,提交并获取post请求中的关键字。
G:\Flask\get_post\app.py
# post请求与获取提交的关键字
@app.route('/login/',methods=["POST","GET"])
def login():
if request.method == "GET":
return render_template('login.html')
else:
username = request.form.get('username')
password = request.form.get('password')
return "username:%s /n password:%s" %(username,password)
login.html
<form action="{{ url_for('login') }}" method="post">
<input type="text" name="username" placeholder="请输入用户名"><br>
<input type="password" name="password" placeholder="请输入密码"><br>
<input type="submit" name="username" value="登录">
</form>