class DevelopmentConfig(Config): DEBUG = True MAIL_SERVER = 'smtp.163.com' MAIL_PORT = 587 # 国内邮件使用SSL协议 MAIL_USE_TLS = False MAIL_USE_SSL = True MAIL_USERNAME = config_info.get('MAIL_USERNAME') MAIL_PASSWORD = config_info.get('MAIL_PASSWORD') SQLALCHEMY_DATABASE_URI = config_info.get('DEV_DATABASE_URL') or \ 'mysql+pymysql://username:yourPassword@host:port/database?charset=utf8'
class Config: """通用配置,根据需要还可添加其他配置类""" # FlaskForm表单为了防止CSRF攻击而设置的密钥 SECRET_KEY = config_info.get('SECRET_KEY') or 'hard to guess string' # 每次请求结束后都会自动提交数据库中的变动 SQLALCHEMY_TRACK_MODIFICATIONS = False # 邮件的主题前缀 FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]' FLASKY_MAIL_SENDER = '杜雄<*****@*****.**>' # 邮件接收者 FLASKY_ADMIN = config_info.get('FLASKY_ADMIN') @staticmethod def init_app(app): pass
class ProductionConfig(Config): SQLALCHEMY_DATABASE_URI = config_info.get('DATABASE_URL') or \ 'sqlite:///' + os.path.join(basedir, 'data.sqlite')
class TestingConfig(Config): TESTING = True SQLALCHEMY_DATABASE_URI = config_info.get('TEST_DATABASE_URL') or \ 'mysql+pymsql://username:yourPassword@host:port/database?charset=utf8'
# myFlask的绝对路径 basedir = os.path.abspath(os.path.dirname(__file__)) app = Flask(__name__) # FlaskForm为了避免CSRF攻击,必须设置密钥 app.config['SECRET_KEY'] = 'hard to guress string' # sqlite数据库路径 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( basedir, 'data.sqlite') app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # 邮箱服务器地址 app.config['MAIL_SERVER'] = 'smtp.163.com' app.config['MAIL_PORT'] = 465 app.config['MAIL_USE_tls'] = False app.config['MAIL_USE_SSL'] = True # 邮箱的账号和密码不能直接写入脚本中,如要中脚本环境中导入敏感信息 app.config['MAIL_USERNAME'] = config_info.get('MAIL_USERNAME') app.config['MAIL_PASSWORD'] = config_info.get('MAIL_PASSWORD') # 设置邮件前缀和发送者 app.config['FLASKY_MAIL_SUBJECT_PREFIX'] = '[Flasky]' app.config['FLASKY_MAIL_SENDER'] = '杜雄<*****@*****.**>' # 从脚本环境中取得邮件接收者 app.config['FLASKY_ADMIN'] = config_info.get('FLASKY_ADMIN') bootstrap = Bootstrap(app) moment = Moment(app) db = SQLAlchemy(app) migrate = Migrate(app) mail = Mail(app) class NameForm(FlaskForm): name = StringField('What is your name?', validators=[DataRequired()])