简体   繁体   中英

Issue with Hiding Django SECRET_KEY Using environment variables

I am running my programs and Django Project on an AWS EC2 Ubuntu instance in a virtual environment. The django project is run on an apache server. For purposes of this question I will say that my secret key is 'AAAA'. I have an environment variable set ("SECRET_KEY"). It is properly setup, and when I put:

import os print(os.environ['SECRET_KEY']) in the python shell, it prints the proper secret key, AAAA. Therefore, I know python is able to access the proper environment variable. In settings.py, when SECRET_KEY='AAAA' , the project works, and everything executes properly. However when I change this to: SECRET_KEY=os.environ['SECRET_KEY'] the server doesn't work (500 Internal Server Error).

Everything seems like it should work. Any suggestions or ideas on what I missed here?

Turns out I had to add a default state to the os.environ.get like so:

SECRET_KEY = os.environ.get('SECRET_KEY', 'mmglfamx3n927*93$ks#r)h%*a(@))vb7*=2q$&z(=6@q&*ghj')

I use it like this SECRET_KEY = os.getenv('SECRET_KEY') to get environment variables in my projects.

Another thing that you can do is print the env variable in the terminal server to make sure it's available for the application.

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM