In my settings, I'm using django-environ to set the key:
import environ
env = environ.Env()
SECRET_KEY = env.read_env('SECRET_KEY')
At the root of my project, I do have a .env file with the actual secret key:
.env:
SECRET_KEY=qgw6s66n3e$27mmddfua*8yq6n%gz(!mx8e=@zbixk50-h020
The error will run during collectstatic
. Here's the traceback:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/core/management/__init__.py", line 194, in fetch_command
settings.INSTALLED_APPS
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/sju/Dev/django/blog-api/src/blog/settings/base.py", line 27, in <module>
SECRET_KEY = env('SECRET_KEY')
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/environ/environ.py", line 130, in __call__
return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
File "/Users/sju/.virtualenvs/blog-api/lib/python2.7/site-packages/environ/environ.py", line 275, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the SECRET_KEY environment variable
What am I missing?
I think you are using it wrong. Based on the documentation you first need to load the .env
file and then use it.
Try this:
import environ
env = environ.Env()
environ.Env.read_env()
SECRET_KEY = env('SECRET_KEY')
Also make sure the .env
file is located in the directory as the settings file. Alternatively, you can pass explicitly pass the location of the file to read_csv()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With