- Web Development with Django Cookbook(Second Edition)
- Aidas Bendoraitis
- 278字
- 2021-07-23 14:31:58
Creating and including local settings
Configuration doesn't necessarily need to be complex. If you want to keep things simple, you can work with two settings files: settings.py
for common configuration and local_settings.py
for sensitive settings that shouldn't be under version control.
Getting ready
Most of the settings for different environments will be shared and saved in version control. However, there will be some settings that are specific to the environment of the project instance, for example, database or e-mail settings. We will put them in the local_settings.py
file.
How to do it…
To use local settings in your project, perform the following steps:
- At the end of
settings.py
, add a version oflocal_settings.py
that claims to be in the same directory, as follows:# settings.py # … put this at the end of the file … try: execfile(os.path.join( os.path.dirname(__file__), "local_settings.py" )) except IOError: pass
- Create
local_settings.py
and put your environment-specific settings there, as shown in the following:# local_settings.py DATABASES = { "default": { "ENGINE": "django.db.backends.mysql", "NAME": "myproject", "USER": "root", "PASSWORD": "root", } } EMAIL_BACKEND = \ "django.core.mail.backends.console.EmailBackend" INSTALLED_APPS += ( "debug_toolbar", )
How it works…
As you can see, the local settings are not normally imported, they are rather included and executed in the settings.py
file itself. This allows you to not only create or overwrite the existing settings, but also adjust the tuples or lists from the settings.py
file. For example, we add debug_toolbar
to INSTALLED_APPS
here in order to be able to debug the SQL queries, template context variables, and so on.
See also
- The Creating a project file structure recipe
- The Toggling the Debug Toolbar recipe in Chapter 10, Bells and Whistles
- Vue.js 3.x快速入門
- 黑客攻防從入門到精通(實戰秘笈版)
- Photoshop智能手機APP UI設計之道
- jQuery EasyUI網站開發實戰
- Cassandra Design Patterns(Second Edition)
- 精通Python設計模式(第2版)
- Clojure Reactive Programming
- C語言程序設計實驗指導 (第2版)
- Apache Solr PHP Integration
- ABAQUS6.14中文版有限元分析與實例詳解
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)
- Yii2 By Example
- 企業級Java現代化:寫給開發者的云原生簡明指南
- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)
- 美麗洞察力:從化妝品行業看顧客需求洞察