简体   繁体   中英

Django settings SECRET_KEY

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

  1. If I put print(os.getcwd()) inside base.py I get /media/grimel/Home/project/ instead of /media/grimel/Home/project/project/settings/
  2. This code works only if I replace:
    key = "secrets.json"
    by
    key = "project/settings/secrets.json"

Personally, I don't like this solution.
So, questions:

  1. Why, for base.py current working directory is so confusing?
  2. What's a better approach in solving this problem?

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")

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.

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