Django settings.py SECRET_KEY error

If you import the django module in settings.py, you will get angry because there is no SECRET_KEY setting when replacing settings.

Background 1

In settings.py ʻadd_to_builtins ('bootstrap3.templatetags.bootstrap3') If you write like this, you don't need to write{% load bootstrap3%}` at the beginning of the template. That's why I wrote it immediately.

from django.template.base import add_to_builtins

add_to_builtins('bootstrap3.templatetags.bootstrap3')

Background 2

django can change the settings it uses by passing the --settings option when executing the manage.py command. By using this, prepare the settings for test and the settings for deploy, and when starting in the test environment or production environment $ python manage.py runserver --settings=myproject.settings_production Execute as follows.

Reference: In the case of gunicorn, it looks like this $ gunicorn -w 2 myproject.wsgi:application --settings=myproject.settings_production --bind=unix:///tmp/myproject.sock

Since the settings for the production environment are different from the DEBUG flag and DB, Import the default settings as shown below and overwrite only the necessary variables.

from .settings import *

DEBUG = False
ALLOWED_HOSTS = ['*']

DATABASES = {
    'default': {
        ...
    }
}

problem

Background 1 (import django.template.base in settings.py) and background 2 (replace settings) The following error is thrown and dropped. import django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Cause

I called django in settings, and then called settings in it, so It seems that the import of default settings was suppressed by infinite loop prevention.

スクリーンショット 2015-07-15 12.31.37.png

counter-measure

Execute ʻadd_to_builtinsin settings.py seems to be bad, so move tomyproject / __ init__.py. However, if you run it with gunicorn, you may get angry if you don't have the DJANGO_SETTINGS_MODULE environment variable when you import django in myproject / __ init__.py` (depending on the module you load).

ImproperlyConfigured: Requested setting BOOTSTRAP3, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

\ # bootstrap3 seems to read settings inside.

So, the contents of myproject / __ init__.py considering the start of gunicorn are like this.

import os

from django.template.base import add_to_builtins


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mailer.settings")

add_to_builtins('bootstrap3.templatetags.bootstrap3')

Recommended Posts

Django settings.py SECRET_KEY error
Django ~ settings.py edition ~
Django2.2 SQLite3 version error
Django Server Error (500) Strategy [2019 Adcare]
Django
[Django] Create your own 403, 404, 500 error pages
Error handling during Django migrate'DIRS': [BASE_DIR /'templates']
[Django] Error encountered when deploying heroku Part 2
Django novice addicted error and solution notes
Permission error when reading Django upload files