1. 程式人生 > 實用技巧 >flask自動重啟與配置檔案匯入,路由重定向(8)

flask自動重啟與配置檔案匯入,路由重定向(8)

在pycharm的配置

1)

檔案:

app.py

from flask import Flask
# from config import DEBUG 配置檔案可以這樣直接匯入

app = Flask(__name__)
app.config.from_object('./config') # 第二種載入配置檔案的方法

@app.route("/hello")
def hello():
    return "ok"
app.run(host='0.0.0.0', port=81, debug=app.config['DEBUG']) # 直接訪問配置檔案,像字典一樣訪問

config.py與app.py在同一級目錄

DEBUG=True
有兩種匯入配置檔案的方式,第一種直接import匯入,第二中用app.config.form_object,但是用第二種方式的時候注意,配置檔案裡面的項需要是大寫,如果實現是小寫會報錯

路由重定向

from flask import Flask,make_response

app = Flask(__name__)

@app.route('/hello/')
def hello_world():
    header={
        'Content-Type': 'text / plain',
        'location': 'https://www.baidu.com
' } return 'Hello World!',301,header if __name__ == '__main__': app.run(host='0.0.0.0',debug=True,port=80)

結果: