Distributing Django projects with unique SECRET_KEYs

前端 未结 10 972
长发绾君心
长发绾君心 2020-12-22 16:41

I have a Django project that I\'d like to distribute on a public repository like bitbucket or github. I\'d like it to be as easy to install as possible, so I\'m including t

10条回答
  •  醉酒成梦
    2020-12-22 16:58

    I'd go about it this way:

    Have the secret key in a separate file "secret_key.py". This file does not exist for a pristine installation. In your settings.py include something like:

    try:
        from .secret_key import SECRET_KEY
    except ImportError:
        SETTINGS_DIR = os.path.abspath(os.path.dirname(__file__))
        generate_secret_key(os.path.join(SETTINGS_DIR, 'secret_key.py'))
        from .secret_key import SECRET_KEY
    

    The function generate_secret_key(filename) that you will write generates a file called filename (which, as we call it, will be secret_key.py in the same dir as settings.py) with the contents:

    SECRET_KEY = '....random string....'
    

    Where random string is the generated key based on a random number.

    For key generation you can use Umang's suggestion https://stackoverflow.com/a/16630719/166761.

提交回复
热议问题