Reputation: 2320
I have the following structure of my project
project
--project
----settings
------base.py
------development.py
------testing.py
------secrets.json
--functional_tests
--manage.py
development.py and testing.py 'inherit' from base.py
from .base import *
So, where I have problems
I have the SECRET_KEY for Django in secrets.json, which is stored in settings folder
I load this key like this (saw this in "Two scoops of Django")
import json
from django.core.exceptions import ImproperlyConfigured
key = "secrets.json"
with open(key) as f:
secrets = json.loads(f.read())
def get_secret(setting, secret=secrets):
try:
return secrets[setting]
except KeyError:
error_msg = "Set the {} environment variable".format(setting)
raise ImproperlyConfigured(error_msg)
SECRET_KEY = get_secret("SECRET_KEY")
But when I run python manage.py runserver
Blah-blah-blah
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
After some investigations I got the following
print(os.getcwd())
inside base.py I get /media/grimel/Home/project/
instead of /media/grimel/Home/project/project/settings/
key = "secrets.json"
key = "project/settings/secrets.json"
Personally, I don't like this solution.
So, questions:
Upvotes: 0
Views: 2684
Reputation: 111
To make life simpler why not move secrets.json
to your project root and reference
import os
key = os.path.join(BASE_DIR, "secrets.json")
directly. This is platform independent saving you the need to override BASE_DIR
at all in your settings file. Don't forget to add your settings file to version control.
Upvotes: 0
Reputation: 490
The working directory is based on how you run the program, in your case python manage.py runserver
hints that your working directory is the one containing manage.py
. Beware that this can vary when run as WSGI script or otherwise, so your concern with using key = "project/settings/secrets.json"
is valid.
One solution is to use the value of __file__
in base.py
, likely to be "project/settings/base.py"
. I would use something like
import os
BASE_DIR = os.path.dirname(__file__)
key = os.path.join(BASE_DIR, "secrets.json")
Upvotes: 3