亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python的flask常用函數(shù)route()

 更新時(shí)間:2022年07月14日 17:05:10   投稿:hqx  
這篇文章主要介紹了Python的flask常用函數(shù)route(),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

一、route()路由概述

  • 功能:將URL綁定到函數(shù)
  • 路由函數(shù)route()的調(diào)用有兩種方式:靜態(tài)路由和動(dòng)態(tài)路由

二、靜態(tài)路由和動(dòng)態(tài)路徑

方式1:靜態(tài)路由

@app.route(“/xxx”) xxx為靜態(tài)路徑 如::/index / /base等,可以返回一個(gè)值、字符串、頁(yè)面等

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:動(dòng)態(tài)路由

采用<>進(jìn)行動(dòng)態(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
  • 則會(huì)在頁(yè)面顯示:Hello w3cschool!

三、route()其它參數(shù)

1.methods=[‘GET’,‘POST’]

  • 當(dāng)前視圖函數(shù)支持的請(qǐng)求方式,不設(shè)置默認(rèn)為GET
  • 請(qǐng)求方式不區(qū)分大小寫
    • methods=[‘GET’] 支持的請(qǐng)求方法為GET
    • methods=[‘POST’] 支持的請(qǐng)求方法為POST
    • methods=[‘GET’,‘POST’] 支持的請(qǐng)求方法為POST GET
  @app.route('/login', methods=['GET', 'POST'])  # 請(qǐng)求參數(shù)設(shè)置不區(qū)分大小寫,源碼中自動(dòng)進(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論