日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

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

更新時間: 2022-09-07 編程語言

一、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!!!'

原文鏈接:https://blog.csdn.net/baidu_24752135/article/details/125685741

欄目分類
最近更新