Flask之Flask例項有哪些引數
阿新 • • 發佈:2018-12-17
常用的引數應用例項
from flask import Flask, render_template, url_for, session, request, redirect app = Flask(__name__, template_folder="templates111", static_folder="jingtaimulu", static_url_path='/static') # √ app.secret_key = "wang" def confirm(func): def inner(*args, **kwargs):if session.get('auth'): return func(*args, **kwargs) else: next_url = request.path[1:] return redirect(url_for("login") + f"?next={next_url}") return inner @app.route('/', endpoint="index") @confirm def index(): return "index" @app.route('/login', methods=["GET", "POST"]) def login(): msg = "" if request.method == "POST": auth = request.form.get("auth") if auth: session["auth"] = auth next_url = request.args.get("next", "index") return redirect(url_for(next_url))else: msg = "error" return render_template("login.html", msg=msg) @app.route('/shopping/', endpoint="shopping") def shopping(): return "Shopping" if __name__ == '__main__': app.run(debug=True)
引數解析
class Flask(_PackageBoundObject)
def __init__( self, import_name, static_url_path=None, # 靜態檔案的訪問路徑, 就相當於別名, 類似於django中的 {% load static %}, 資源的url開頭就是這裡指定的路徑 static_folder='static', # 靜態檔案目錄的路徑 預設當前專案中的static目錄 static_host=None, # 遠端靜態檔案所用的Host地址, 如CDN的主機地址 host_matching=False, # 是否開啟host主機位匹配,是要與static_host一起使用,如果配置了static_host, 則必須賦值為True subdomain_matching=False, # 理論上來說是用來限制SERVER_NAME子域名的,但是目前還沒有感覺出來區別在哪裡 template_folder='templates', # template模板目錄, 預設當前專案中的 templates 目錄 instance_path=None, # 指向另一個Flask例項的路徑 instance_relative_config=False, # 是否載入另一個例項的配置 root_path=None # 主模組所在的目錄的絕對路徑,預設專案目錄