Flask配置文件

栏目: Python · 发布时间: 7年前

内容简介:flask设置配置的的方式有三种方法一:直接在代码中使用app.config[‘配置名’] 的方式app.debug = True 等价于 app.config[‘debug’] = True,但是只要少部分参数支持app.debug = True的方式

flask设置配置的的方式有三种

方法一:直接在代码中使用app.config[‘配置名’] 的方式

app.debug = True 等价于 app.config[‘debug’] = True,但是只要少部分参数支持app.debug = True的方式

from flask import Flask


app = Flask(__name__)
# flask 配置文件
app.debug = True
# app.config['debug'] = True
app.secret_key = "password"
# app.config['secret_key'] = "password"

@app.route('/')
def index():
    return 'Hellow World!'


if __name__ == '__main__':
    app.run(

方法二:使用 app.config.from_pyfile(“python文件名称”)  读取一个py文件的方式导入

创建一个settings.py 文件内容如下,配置文件的变量名都必须是大写的

DEBUG = True

再调用代码中输入如下

app.config.from_pyfile("settings.py")

方式三:app.config.from_object(“python类或类的路径”)

创建一个 settings.py文件,内容如下

class Config(object):
    DEBUG = False
    TESTING = False
    DATABASE_URI = 'sqlite://:memory:'
class ProductionConfig(Config):
    DATABASE_URI = 'mysql://user@localhost/foo'
class DevelopmentConfig(Config):
    DEBUG = True
class TestingConfig(Config):
    TESTING = True

在代码中导入settings.py 文件

app.config.from_object("settings.ProductionConfig")

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Implementing Responsive Design

Implementing Responsive Design

Tim Kadlec / New Riders / 2012-7-31 / GBP 27.99

New devices and platforms emerge daily. Browsers iterate at a remarkable pace. Faced with this volatile landscape we can either struggle for control or we can embrace the inherent flexibility of the w......一起来看看 《Implementing Responsive Design》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具