Tutorial on configuring an app in the Python web framework

Source: Internet
Author: User
This article describes how to configure an app in the Python web framework. the code is based on Python2.x. For more information, see the Web framework and ORM framework, we can start assembling the App.

Generally, a Web App needs to read the configuration file when running, such as the database username and password, and run it in different environments, the Web App can obtain the correct configuration by reading different configuration files.

Because Python syntax is simple, you can directly use Python source code for configuration without parsing a separate configuration file, such as. properties or. yaml.

The default configuration file should fully comply with the local development environment, so that the server can be started immediately without any settings.

We name the default configuration file config_default.py:

# config_default.pyconfigs = {  'db': {    'host': '127.0.0.1',    'port': 3306,    'user': 'www-data',    'password': 'www-data',    'database': 'awesome'  },  'session': {    'secret': 'AwEsOmE'  }}

The preceding configuration file is simple and clear. However, if you want to deploy the service to the server, you usually need to modify the host and other information of the database. modifying config_default.py directly is not a good solution. a better way is to write config_override.py to overwrite some default settings:

# config_override.pyconfigs = {  'db': {    'host': '192.168.0.100'  }}

Using config_default.py as the standard configuration of the development environment and config_override.py as the standard configuration of the production environment, we can conveniently develop the application locally and deploy the application on the server at any time.

The application must first read the configuration file from config_override.py. To simplify the reading of the configuration file, you can read all the configurations to the unified config. py:

# config.pyconfigs = config_default.configstry:  import config_override  configs = merge(configs, config_override.configs)except ImportError:  pass

In this way, the App configuration is completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.