Python的flask常用函數(shù)route()
發(fā)布日期:2022-07-20 19:04 | 文章來源:gibhub
一、route()路由概述
- 功能:將URL綁定到函數(shù)
- 路由函數(shù)route()的調(diào)用有兩種方式:靜態(tài)路由和動態(tài)路由
二、靜態(tài)路由和動態(tài)路徑
方式1:靜態(tài)路由
@app.route(“/xxx”) xxx為靜態(tài)路徑 如::/index / /base等,可以返回一個值、字符串、頁面等
from flask import Flask app = Flask(__name__) @app.route('/hello') def hello_world(): return 'Hello World!!!' @app.route('/pro') def index(): return render_template('login.html') if __name__ == '__main__': app.run(debug = True)
方式2:動態(tài)路由
采用<>進(jìn)行動態(tài)url的傳遞
@app.route(“/”),這里xxx為不確定的路徑。
from flask import Flask app = Flask(__name__) @app.route('/hello/<name>') def hello_name(name): return 'Hello %s!' % name if __name__ == '__main__': app.run(debug = True)
- 如果瀏覽器地址欄輸入:
http:// localhost:5000/hello/w3cschool
- 則會在頁面顯示:
Hello w3cschool!
三、route()其它參數(shù)
1.methods=[‘GET’,‘POST’]
- 當(dāng)前視圖函數(shù)支持的請求方式,不設(shè)置默認(rèn)為GET
- 請求方式不區(qū)分大小寫
- methods=[‘GET’] 支持的請求方法為GET
- methods=[‘POST’] 支持的請求方法為POST
- methods=[‘GET’,‘POST’] 支持的請求方法為POST GET
@app.route('/login', methods=['GET', 'POST']) # 請求參數(shù)設(shè)置不區(qū)分大小寫,源碼中自動進(jìn)行了upper def login(): if request.method == 'GET': return render_template('login.html') elif request.method == 'POST': username = request.form.get('username') pwd = request.form.get('pwd') if username == 'yang' and pwd == '123456': session['username'] = username return 'login successed 200 ok!' else: return 'login failed!!!'
到此這篇關(guān)于Python的flask常用函數(shù)route()的文章就介紹到這了,更多相關(guān)Python flask 內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章