I added a SECRET_KEY config variable to my Django app on Heroku but now it won't work locally

前端 未结 3 1429
名媛妹妹
名媛妹妹 2021-01-15 02:59

I changed my secret key to an environment variable on my Heroku app. I changed it because I found out that keeping the secret key in settings.py was a security

相关标签:
3条回答
  • 2021-01-15 03:35

    I had a bit of trouble working this out too, found my answer here: Set up your local environment variables

    If you are using heroku local to develop locally, this might work for you. I just needed to include this line in an .env file placed in the top directory with my Procfile:

    SECRET_KEY = 'yourkey'
    

    And in settings.py:

    os.environ.get('SECRET_KEY')
    

    This works great for me. Otherwise if for whatever reason you aren't using heroku local, maybe you could try importing your key from another file when working locally (and placing this file in your gitignore), and swapping back to the heroku config variable for deployment.

    0 讨论(0)
  • 2021-01-15 03:41

    You have to set your environmental variables in your development environment.

    Windows

    Go to Computer > Properties > Advanced System Settings.
    Go to the Advanced tab, and at the bottom there is an Environment Variables... button.

    In there you can edit the variables as you like.

    Linux

    Edit /etc/environment to include:

    SECRET_KEY = <yoursecretkey>
    

    or

    You should be using a virtual environment to isolate your system Python installation from your different projects (it solves conflicting version requirements) and to make deployment easier. Virtualenv Tutorial

    To activate your virtual environment when you want to use it there is a shell script located at <your_virtualenv>/bin/activate that handles changing all the environmental variables that make the virtual environment work.

    Add:

    SECRET_KEY='<yoursecretkey>'
    export SECRET_KEY
    

    to the bottom of the activate file and when it is run it will add (export) the environmental variable.

    0 讨论(0)
  • 2021-01-15 03:44

    You can export your secret key as an environment variable locally.

    export SECRET_KEY=mysecretkey
    ./manage.py runserver
    

    Or you could change your settings.py to use a hardcoded secret key in DEBUG mode. If you do this, make sure you are running with DEBUG = False on Heroku.

    import os
    
    if DEBUG:
        SECRET_KEY = 'mysecretkey'
    else:
        SECRET_KEY = os.environ['SECRET_KEY']
    
    0 讨论(0)
提交回复
热议问题