python框架Flask学习笔记之get和post请求
生活随笔
收集整理的這篇文章主要介紹了
python框架Flask学习笔记之get和post请求
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.get請(qǐng)求:
使用場(chǎng)景:
說(shuō)白了就是從服務(wù)器獲取數(shù)據(jù),比如查詢百度的時(shí)候就是這樣的。
傳參方式:
放在url中并且是通過(guò) "? ?? " 的形式來(lái)指定Key和 Value的。
2.post請(qǐng)求:
使用場(chǎng)景:
對(duì)服務(wù)期產(chǎn)生影響,比如說(shuō)登入的時(shí)候提交密碼。
傳參方式:
不通過(guò)url傳參,通過(guò)"? foem_data? "的形式將信息發(fā)送至服務(wù)器。
?
3.獲取兩種請(qǐng)求的參數(shù)
1.get請(qǐng)求:
flask.request.args獲取,返回的是字典。
2.post請(qǐng)求;
flask.request.form獲取,返回字典。
注意:
默認(rèn)的視圖函數(shù)只能發(fā)送get請(qǐng)求。如果要發(fā)送post請(qǐng)求時(shí)要再參數(shù)中寫(xiě)清楚。
例如:@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!'
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/nimingdaoyou/p/9037812.html
總結(jié)
以上是生活随笔為你收集整理的python框架Flask学习笔记之get和post请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 对于缓存的理解
- 下一篇: GitLab的安装及使用教程