Python的flask常用函數(shù)route()
更新時間:2022年07月14日 17:05:10 投稿:hqx
這篇文章主要介紹了Python的flask常用函數(shù)route(),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
一、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)路由
采用<>進行動態(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è)置默認為GET
- 請求方式不區(qū)分大小寫
- methods=[‘GET’] 支持的請求方法為GET
- methods=[‘POST’] 支持的請求方法為POST
- methods=[‘GET’,‘POST’] 支持的請求方法為POST GET
@app.route('/login', methods=['GET', 'POST']) # 請求參數(shù)設(shè)置不區(qū)分大小寫,源碼中自動進行了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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)從一組顏色中找出與給定顏色最接近顏色的方法
這篇文章主要介紹了python實現(xiàn)從一組顏色中找出與給定顏色最接近顏色的方法,涉及Python操作rgb格式顏色的技巧,非常具有實用價值,需要的朋友可以參考下2015-03-03
用python3 返回鼠標位置的實現(xiàn)方法(帶界面)
今天小編就為大家分享一篇用python3 返回鼠標位置的實現(xiàn)方法(帶界面),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
pyqt5 tablewidget 利用線程動態(tài)刷新數(shù)據(jù)的方法
今天小編就為大家分享一篇pyqt5 tablewidget 利用線程動態(tài)刷新數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06

