flask的路由配置,特殊裝飾器
阿新 • • 發佈:2019-01-14
1,flask中的路由
- endpoint-url_for反向地址
- endpoint預設是檢視函式名endpoint="雪雪"
- methods 指定檢視函式的請求方式,預設GET
- default={"nid":1}指定檢視函式的預設值
- strict_slashes = False 是否嚴格遵循路由規則/login/
- redirect_to"/xueren/" 永久跳轉地址301
- 動態路由引數
- /<int:nid> / <string:str> / <nid>
- 檢視函式中需要有引數接收動態引數
2,Flask中的例項化配置
- template_folder = "temp" # template模板目錄,預設當前專案中的template目錄
- static_folder = "xuexue"目錄
- static_url_path= "/static" # 訪問路徑
- host_matching = False # 如果不是特別需要,慎用,否則所欲的route都需要host=""引數
- subdomain_matching = False # 理論上來說是用來限制SERVER_NAME子域名,
- instance_path = None. # 指向另一個Flask例項的路徑
- instance_relative_config = Flase # 是否載入另一個例項的配置
- root_path = None # 主模組所在的目錄絕對路徑,預設專案目錄
3,app物件配置
- app.config.from_object(Debug)
- class Debug(object):
- DEBUG = true
4,Blueprint
- from flask import Blueprint
- blue = Blueprint("blue_id", __name__,url_prefix="user"每次訪問藍圖的路徑字首)
- app.reguster_blueprint(blue)
5,特殊裝飾器:
- @app.template_global()
- @app.template_filter()
- @app.before_request # 請求進入檢視函式之前
- @app.after_request
- def af1(response)
- return response # 結束檢視函式之後, 返回客戶端之前
- 正常:be1---be2---be3---af3---af2---af1
- 異常:be1---af3---af2---af1
- @app.errorhandler(404)重定義頁面
- def error404(args):
- return args
- 基於藍圖實現增刪改查資料,基於Before_request session實現使用者校驗,可選errorhandler
具體程式碼如下:
藍圖程式碼:
from flask import Flask, request, render_template, redirect, session
from serv import users
import weishenme
app = Flask(__name__, static_url_path="/static")
# 註冊配置類
app.config.from_object(weishenme.Debug)
# 註冊藍圖
app.register_blueprint(users.user_blue) @app.route("/detail") def detail(): return render_template("detail.html") @app.route("/login", methods=["GET", "POST"]) def login(): if request.method == "GET": return render_template("login1.html") else: session["user"] = request.form.get("user") return redirect("/") @app.route("/") def xiaocai(): return "騷帆是一根小菜" if __name__ == '__main__': app.run(debug=True)
配置類:
class Debug(object):
DEBUG=True
SECRET_KEY="xuexue"
SESSION_COOKIE_NAME="xiaoxue"
class Testing(object):
TESTING=True
SECRET_KEY="xiaopangpang" SESSION_COOKIE_NAME="xueer" class Production(object): SECRET_KEY="xueren" SESSION_COOKIE_NAME="csrf_token"
app的程式碼:
from flask import Flask, session, redirect, url_for, render_template, request
import weishenme
# 例項化一個Flask物件app
app = Flask(__name__) # __name__表示是當前目錄
# 指定session的key是xuexue
app.secret_key = "xuexue"
# 註冊配置類
# app.config.from_object(weishenme.Debug)
app.config.from_object(weishenme.Testing)
# 裝飾器
def outer(func):
def hahaha(*args, **kwargs):
if session["user"]: print(session["user"]) ret = func(*args, **kwargs) return ret else: return redirect("/login") return hahaha @app.route("/<nid>", endpoint="wc", methods=["GET", "POST"]) @outer def wc(nid): print(nid) return render_template("xuexue.html") @app.route("/detail", endpoint="detail") @outer def detail(): print("detail") return render_template("detail.html") @app.route("/login", methods=["GET", "POST"], strict_slashes=False) def login(): if request.method == "GET": return render_template("login1.html") else: session["user"] = request.form.to_dict().get("user") return redirect("/miss") if __name__ == '__main__': # 執行這個flask專案 app.run(host="0.0.0.0", port=2018)
請求執行流程程式碼:
from flask import Flask, request, session, render_template, redirect, session
import weishenme
from serv import users
app = Flask(__name__, static_folder="static_list", static_url_path="/static")
# 到瑞配置類
app.config.from_object(weishenme.Debug)
# 註冊藍圖
app.register_blueprint(users.user_blue) @app.before_request def is_login(): print("b1") if request.path == "/login" : return None elif session.get("user"): return None else: return redirect("/login") @app.before_request def b2(): print("b2") return None @app.before_request def b3(): print("b3") return None @app.after_request def af1(res): print("af1") return res @app.after_request def af2(res): print("af2") return res @app.after_request def af3(res): print("af3") return res @app.route("/", endpoint="index",methods=["POST", "GET"]) def index(): return render_template("index1.html") @app.route("/detail", endpoint="detail") def detail(): return render_template("detail.html") @app.route("/login", methods=["GET", "POST"], strict_slashes=False) def login(): if request.method == "GET": return render_template("login1.html") else: print("xiaoxue") session["user"] = request.form.get("user") return redirect("/") @app.errorhandler(404) def error(args): print(args) return "您訪問的頁面不存在,或者走丟了.....%s" % args if __name__ == '__main__': app.run(debug=True)