How to manage local vs production settings in Django?

前端 未结 22 1532
别跟我提以往
别跟我提以往 2020-11-22 15:00

What is the recommended way of handling settings for local development and the production server? Some of them (like constants, etc) can be changed/accessed in both, but s

相关标签:
22条回答
  • 2020-11-22 15:04

    As an alternative to maintain different file if you wiil: If you are using git or any other VCS to push codes from local to server, what you can do is add the settings file to .gitignore.

    This will allow you to have different content in both places without any problem. SO on server you can configure an independent version of settings.py and any changes made on the local wont reflect on server and vice versa.

    In addition, it will remove the settings.py file from github also, the big fault, which i have seen many newbies doing.

    0 讨论(0)
  • 2020-11-22 15:04

    I found the responses here very helpful. (Has this been more definitively solved? The last response was a year ago.) After considering all the approaches listed, I came up with a solution that I didn't see listed here.

    My criteria were:

    • Everything should be in source control. I don't like fiddly bits lying around.
    • Ideally, keep settings in one file. I forget things if I'm not looking right at them :)
    • No manual edits to deploy. Should be able to test/push/deploy with a single fabric command.
    • Avoid leaking development settings into production.
    • Keep as close as possible to "standard" (*cough*) Django layout as possible.

    I thought switching on the host machine made some sense, but then figured the real issue here is different settings for different environments, and had an aha moment. I put this code at the end of my settings.py file:

    try:
        os.environ['DJANGO_DEVELOPMENT_SERVER'] # throws error if unset
        DEBUG = True
        TEMPLATE_DEBUG = True
        # This is naive but possible. Could also redeclare full app set to control ordering. 
        # Note that it requires a list rather than the generated tuple.
        INSTALLED_APPS.extend([
            'debug_toolbar',
            'django_nose',
        ])
        # Production database settings, alternate static/media paths, etc...
    except KeyError: 
        print 'DJANGO_DEVELOPMENT_SERVER environment var not set; using production settings'
    

    This way, the app defaults to production settings, which means you are explicitly "whitelisting" your development environment. It is much safer to forget to set the environment variable locally than if it were the other way around and you forgot to set something in production and let some dev settings be used.

    When developing locally, either from the shell or in a .bash_profile or wherever:

    $ export DJANGO_DEVELOPMENT_SERVER=yep
    

    (Or if you're developing on Windows, set via the Control Panel or whatever its called these days... Windows always made it so obscure that you could set environment variables.)

    With this approach, the dev settings are all in one (standard) place, and simply override the production ones where needed. Any mucking around with development settings should be completely safe to commit to source control with no impact on production.

    0 讨论(0)
  • 2020-11-22 15:05

    Remember that settings.py is a live code file. Assuming that you don't have DEBUG set on production (which is a best practice), you can do something like:

    if DEBUG:
        STATIC_PATH = /path/to/dev/files
    else:
        STATIC_PATH = /path/to/production/files
    

    Pretty basic, but you could, in theory, go up to any level of complexity based on just the value of DEBUG - or any other variable or code check you wanted to use.

    0 讨论(0)
  • 2020-11-22 15:06

    I think the best solution is suggested by @T. Stone, but I don't know why just don't use the DEBUG flag in Django. I Write the below code for my website:

    if DEBUG:
        from .local_settings import *
    

    Always the simple solutions are better than complex ones.

    0 讨论(0)
  • 2020-11-22 15:08

    In order to use different settings configuration on different environment, create different settings file. And in your deployment script, start the server using --settings=<my-settings.py> parameter, via which you can use different settings on different environment.

    Benefits of using this approach:

    1. Your settings will be modular based on each environment

    2. You may import the master_settings.py containing the base configuration in the environmnet_configuration.py and override the values that you want to change in that environment.

    3. If you have huge team, each developer may have their own local_settings.py which they can add to the code repository without any risk of modifying the server configuration. You can add these local settings to .gitnore if you use git or .hginore if you Mercurial for Version Control (or any other). That way local settings won't even be the part of actual code base keeping it clean.

    0 讨论(0)
  • 2020-11-22 15:09

    There is also Django Classy Settings. I personally am a big fan of it. It's built by one of the most active people on the Django IRC. You would use environment vars to set things.

    http://django-classy-settings.readthedocs.io/en/latest/

    0 讨论(0)
提交回复
热议问题