Flask工程配置加载方式
1.从配置对象中加载
# encoding: utf-8 from flask import Flask class DefalutConfig(object): SECRET_KEY = "1dfjkopiuhhhhhhhhaadd" app = Flask(__name__, static_url_path="/s", static_folder="st") # 1.加载配置类:使用对象配置参数 app.config.from_object(DefalutConfig) @app.route("/") def index(): print(app.config.get("SECRET_KEY")) return "hello" if __name__ == "__main__": app.run()
2.从配置文件中加载
# encoding: utf-8 SECRET_KEY = "1dfjkopiuhhhhhhhhaadd"
# encoding: utf-8 from flask import Flask app = Flask(__name__, static_url_path="/s", static_folder="st") # 1.加载配置文件: app.config.from_pyfile("setting.py") @app.route("/") def index(): print(app.config.get("SECRET_KEY")) return "hello" if __name__ == "__main__": app.run()
3.从环境变量中加载
# encoding: utf-8 SECRET_KEY = "1dfjkopiuhhhhhhhhaadd"
# encoding: utf-8 from flask import Flask app = Flask(__name__, static_url_path="/s", static_folder="st") # 1.加载环境变量: app.config.from_envvar("PROJECT_SETTING") @app.route("/") def index(): print(app.config.get("SECRET_KEY")) return "hello" if __name__ == "__main__": app.run()