1.get請求:
使用場景:
說白了就是從服務器獲取數據,比如查詢百度的時候就是這樣的。
傳參方式:
放在url中並且是通過 " ? " 的形式來指定Key和 Value的。
2.post請求:
使用場景:
對服務期產生影響,比如說登入的時候提交密碼。
傳參方式:
不通過url傳參,通過" foem_data "的形式將信息發送至服務器。
3.獲取兩種請求的參數
1.get請求:
flask.request.args獲取,返回的是字典。
2.post請求;
flask.request.form獲取,返回字典。
注意:
默認的視圖函數只能發送get請求。如果要發送post請求時要再參數中寫清楚。
例如:@app.route('/login/',methods=['POST'])
示例:
1 from flask import Flask,render_template,request 2 3 app = Flask(__name__) 4 5 6 @app.route('/') 7 def index(): 8 return render_template('index.html') 9 10 @app.route('/search/') 11 def search(): 12 print(request.args) 13 return 'search' 14 15 @app.route('/login/',methods=['GET','POST']) 16 def login(): 17 if request.method == 'GET': 18 return render_template('login.html') 19 else: 20 username = request.form.get('username') 21 password = request.form.get('password') 22 print(username) 23 print(password) 24 return 'hello!'