flask 配置文件settings.py(flask 44)

2019-08-15 18:43:22 浏览数 (1)

settings.py

import os

basedir=os.path.abspath(os.path.dirname(file))

class BaseConfig(object): SECRET_KEY=os.getenv('SECRET_KEY','hard to guess')

代码语言:javascript复制
SQLALCHEMY_TRACK_MODIFICATIONS=False

MAIL_SERVER=os.getenv('MAIL_SERVER')
MAIL_PORT=465
MAIL_USE_SSL=True
MAIL_USERNAME=os.getenv('MAIL_USERNAME')
MAIL_PASSWORD=os.getenv('MAIL_PASSWORD')
MAIL_DEFAULT_SENDER=('flask blog',MAIL_USERNAME)

FLASKBLOG_EMAIL=os.getenv('FLASKBLOG_EMAIL')
FLASKBLOG_POST_PER_PAGE=10
FLASKBLOG_MANAGE_PER_PAGE = 10
FLASKBLOG_COMMIT_PER_PAGE = 10

class DevelopmentConfig(BaseConfig): SQLALCHEMY_DATABASE_URI='sqlite:///' os.path.join(basedir,'data-dev.db')

class TestingConfig(BaseConfig): TESTING=True WTF_CSRF_ENABLED=False SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'

class ProductionConfig(BaseConfig): SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI','sqlite:///' os.path.join(basedir,'data.db'))

config={ 'development':DevelopmentConfig, 'testing':TestingConfig, 'production':ProductionConfig }

app.py

from flask import Flask from views.admin import admin_bp from views.auth import auth_bp from views.blog import blog_bp from settings import config

import os app = Flask(name) basedir = os.path.abspath(os.path.dirname(file))

def create_app(config_name=None): if config_name is None: config_name = os.getenv('FLASK_CONFIG', 'development')

代码语言:javascript复制
app = Flask('bluelog')
app.config.from_object(config[config_name])

register_blueprints(app)
return app

def register_blueprints(app): app.register_blueprint(blog_bp) app.register_blueprint(admin_bp) app.register_blueprint(auth_bp)

.flaskenv

FLASK_APP="flaskblog:create_app('development')"

0 人点赞