flask production and development mode

ぐ巨炮叔叔 提交于 2020-03-18 03:34:15

问题


I have developed an application with flask, and I want to publish it for production, but I do not know how to make a separation between the production and development environment (database and code), have you documents to help me or code. I specify in the config.py file the two environment but I do not know how to do with.

class DevelopmentConfig(Config):
    """
    Development configurations
    """
    DEBUG = True
    SQLALCHEMY_ECHO = True
    ASSETS_DEBUG = True
    DATABASE = 'teamprojet_db'
    print('THIS APP IS IN DEBUG MODE. YOU SHOULD NOT SEE THIS IN PRODUCTION.')


class ProductionConfig(Config):
    """
    Production configurations
    """
    DEBUG = False
    DATABASE = 'teamprojet_prod_db'

回答1:


To add onto Daniel's answer:

Flask has a page in its documentation that discusses this very issue.

Since you've specified your configuration in classes, you would load your configuration with app.config.from_object('configmodule.ProductionConfig')




回答2:


One convention used is to specify an environment variable before starting your application.

For example

$ ENV=prod; python run.py

In your app, you check the value of that environment variable to determine which config to use. In your case:

run.py

import os
if os.environ['ENV'] == 'prod':
    config = ProductionConfig()
else:
    config = DevelopmentConfig()

It is also worth noting that the statement

print('THIS APP IS IN DEBUG MODE. YOU SHOULD NOT SEE THIS IN PRODUCTION.')

prints no matter which ENV you set since the interpreter executes all the code in the class definitions before running the rest of the script.



来源:https://stackoverflow.com/questions/44951244/flask-production-and-development-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!