官术网_书友最值得收藏!

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:

  1. At the end of settings.py, add a version of local_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
  2. 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
主站蜘蛛池模板: 三明市| 娄底市| 临猗县| 桦南县| 改则县| 锡林郭勒盟| 虎林市| 潢川县| 昆明市| 务川| 三明市| 朔州市| 定南县| 临朐县| 乌拉特前旗| 伊春市| 临洮县| 阿荣旗| 尤溪县| 台山市| 威远县| 都兰县| 榆社县| 永州市| 金寨县| 南澳县| 兴山县| 建水县| 集安市| 遵义县| 吉木乃县| 桓仁| 商城县| 浏阳市| 茂名市| 苏尼特右旗| 合肥市| 普兰店市| 城市| 肇东市| 襄城县|