How to manage local vs production settings in Django?

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

    I had my settings split as follows

    settings/
         |
         |- base.py
         |- dev.py
         |- prod.py  
    

    We have 3 environments

    • dev
    • staging
    • production

    Now obviously staging and production should have the maximum possible similar environment. So we kept prod.py for both.

    But there was a case where I had to identify running server is a production server. @T. Stone 's answer helped me write check as follows.

    from socket import gethostname, gethostbyname  
    PROD_HOSTS = ["webserver1", "webserver2"]
    
    DEBUG = False
    ALLOWED_HOSTS = [gethostname(), gethostbyname(gethostname()),]
    
    
    if any(host in PROD_HOSTS for host in ALLOWED_HOSTS):
        SESSION_COOKIE_SECURE = True
        CSRF_COOKIE_SECURE = True  
    

提交回复
热议问题