1 修改/新增配置項的3種方法
# 配置參數的使用方式 # 1. 使用配置文件 # app.config.from_pyfile("config.cfg") # 2. 使用對象配置參數 class Config(object): DEBUG = True ITCAST = "python" app.config.from_object(Config) # # 3. 直接操作config的字典對象 # app.config["DEBUG"] = True
2 讀取配置項的2種方法
第二種方法需導入current_app。它是app對象的別名(相當於app對象的全局代理人)
from flask import Flask, current_app
@app.route("/") def index(): """定義的視圖函數""" # a = 1 / 0 # 讀取配置參數 # 1. 直接從全局對象app的config字典中取值 # print(app.config.get("ITCAST")) # 2. 通過current_app獲取參數 print(current_app.config.get("ITCAST")) return "hello flask"