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()
posted @ 2020-08-18 16:44  朵渔  阅读(177)  评论(0编辑  收藏  举报