How to manage local vs production settings in Django?

前端 未结 22 2092
别跟我提以往
别跟我提以往 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:11

    1 - Create a new folder inside your app and name settings to it.

    2 - Now create a new __init__.py file in it and inside it write

    from .base import *
    
    try:
        from .local import *
    except:
        pass
    
    try:
        from .production import *
    except:
        pass
    

    3 - Create three new files in the settings folder name local.py and production.py and base.py.

    4 - Inside base.py, copy all the content of previous settings.py folder and rename it with something different, let's say old_settings.py.

    5 - In base.py change your BASE_DIR path to point to your new path of setting

    Old path-> BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    New path -> BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    This way, the project dir can be structured and can be manageable among production and local development.

提交回复
热议问题