This article describes how to deploy the settings file in the Python Django framework, including some simple analysis of the disadvantages of local_settings, for more information, see django's lack of necessary standards in the directory structure division of a project, in addition, it is hard to say who is doing better. According to my project organization habits, I released a project dj-scaffold.
A few days ago I made an "advertisement" for my project dj-scaffold on reddit (see http://redd.it/kw5d4 ). I don't want to make a bad comments, or even get a negative score. The project is not worth mentioning. Although it is uncomfortable to face negative voices, constructive comments need to be listened to. Those purely personal preferences are automatically filtered out.
Coderanger recommends that you refer to The Best (and Worst) of Django practices when talking about how to organize The settings file. The main point in this article is that the configuration of the development environment and production environment must be put into VCS for version control. I have made some adjustments to the settings module. Note: Code https://github.com/vicalloy/dj-scaffold/tree/master/dj_scaffold/conf/prj/sites/settings
Disadvantages of local_settings
To distinguish the default configuration of a project from the local configuration, add a local_settings.py file and import the file at the end of the settings file.
try: from local_settings import *except: pass
The problem is that you cannot control the version of local_settings.py. If the configuration of the deployment environment is lost, it will be difficult to retrieve it.
Solution
The recommended solution is as follows:
Reasonable configuration file organization
The Code is as follows:
| ~ Settings/
|-_ Init _. py
|-Base. py # default configuration information
|-Dev. py # Development Environment Configuration
|-Local. sample # import the local extension configuration at the end of dev and production
|-Pre. sample # set the current configuration to the production environment or development environment
| '-Production. py # production environment Configuration
Usage
DJANGO_SETTINGS_MODULE
The admin script of django provides the settings parameter to specify the currently used configuration file.
django-admin.py shell --settings=settings.dev
In the wsgi script, you can directly set the settings to be used.
deploy.wsgios.environ['DJANGO_SETTINGS_MODULE'] = settings.production
Simplified Parameters
Of course, it is annoying to include the settings parameter every time you use the django-admin.py, so it is recommended to configure the configuration file you need in pre. py.
SETTINGS = 'production' #dev