This article focuses on the deployment recommendations for the settings file in the Python Django framework, including a simple analysis of the local_settings's ills, and the need for a friend to refer to the
Django lacks the necessary specification for the directory structure of a project, so different people have strange ways of organizing their projects, and it's hard to say who's doing better. I published a project dj-scaffold according to my own project organization habits.
A few days ago on the Reddit for my project Dj-scaffold played an "ad" (see: HTTP://REDD.IT/KW5D4). Don't want to judge very bad, even almost be beaten into negative points. More people have said the project is worthless. Although the negative voice may be a bit unpleasant, but the constructive comments are still to be heard, as for those who are purely personal preference part of the automatic filtering.
When talking about how the settings file is organized, Coderanger recommends referencing the practice in the best (and worst) of Django. The main idea in this paper is that the development environment and the configuration of the production environment need to be put into the VCs for versioning. I have made some adjustments to the settings module for the purposes of this article. Note: Code https://github.com/vicalloy/dj-scaffold/tree/master/dj_scaffold/conf/prj/sites/settings
The ills of local_settings
The most common practice for separating the project's default configuration from the local configuration is to add a local_settings.py file and import the file at the end of the settings file.
?
1 2 3 4 |
Try:from local_settings Import * except:pass |
The problem with this is that you can't have version control of local_settings.py, and the configuration of your deployment environment will be hard to find if you lose it.
Solution
For this issue, the proposed solution is as follows
Reasonable configuration file organization method
Copy code code as follows:
|~settings/
| |-__init__.py
| |-base.py #默认配置信息
| |-dev.py #开发环境的配置
| |-local.sample #本地的扩展配置在dev和production的最后进行import
| |-pre.sample #设置当前使用的配置为生产环境还是开发环境
| '-production.py #生产环境的配置
How to use
?
1 |
<strong>DJANGO_SETTINGS_MODULE</strong> |
The Django Admin script provides the settings parameter for specifying the currently used configuration file
?
1 |
django-admin.py Shell--settings=settings.dev |
In the Wsgi script, you can set the settings you want to use directly
?
1 2 |
Deploy.wsgi os.environ[' django_settings_module '] = settings.production |
Simplifying parameters
Of course, if you're using the settings parameter every time you use django-admin.py, it's really annoying, so it's recommended that you configure the configuration file you want to use in pre.py.
?
1 |
SETTINGS = ' production ' #dev |