I am just deploying my django application and when I try to access it the application throws the following Error:
Traceback:
...
File "/home/sysadmin/public_html/aegee-stuttgart.org/aegeewww/settings.py", line 23, in <module>
SECRET_KEY = os.environ['SECRET_KEY']
File "/usr/lib/python2.7/UserDict.py", line 40, in __getitem__
raise KeyError(key)
KeyError: 'SECRET_KEY'
The Key is being exportet with .bashrc and when I try to access it in console with
import os
print(os.environ['SECRET_KEY'])
I get the right key.
As of this thread, it is not possible to make env vars accessible to django with .bashrc.
But how do I do it then?
Edit: wsgi.py:
import os, sys
# add the aegeewww project path into the sys.path
sys.path.append('/home/sysadmin/public_html/aegee-stuttgart.org/aegeewww')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/sysadmin/.virtualenvs/django/lib/python2.7site-packages')
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aegeewww.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I have managed It with your help! Thank you all a lot!
For future references:
At first set the Keys in your apache configuration like this:
<VirtualHost *:80>
...
SetEnv KEY_TO_STORE keyvalue
...
</VirtualHost>
I changed my wsgi.py as suggested in this thread by using the WSGIEnvironment class.
In there you have to add
os.environ['KEY_TO_STORE'] = environ['KEY_TO_STORE']
to make the wsgi environ variables accessible with os.environment in settings.py
my updated wsgy.py:
import os, sys
# add the aegeewww project path into the sys.path
sys.path.append('/home/sysadmin/public_html/aegee-stuttgart.org/aegeewww')
# add the virtualenv site-packages path to the sys.path
sys.path.append('/home/sysadmin/.virtualenvs/django/lib/python2.7/site-packages')
import django
from django.core.handlers.wsgi import WSGIHandler
class WSGIEnvironment(WSGIHandler):
def __call__(self, environ, start_response):
# let django recognize env variables
os.environ['SECRET_KEY'] = environ['SECRET_KEY']
# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aegeewww.settings")
django.setup()
return super(WSGIEnvironment, self).__call__(environ, start_response)
application = WSGIEnvironment()