Forums

raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

I have problem with my new deployed app, a blog made in wagtail. It can not find any secret key. I have tried to create an .env file to store the key, but it seems my is not working.

This is the code of wsgi.py :

import os
import sys
from django.core.wsgi import get_wsgi_application
from dotenv import load_dotenv

# add your project directory to the sys.path
project_home = '/home/Tecnophilosophy/tecnophilosophy_blog'
if project_home not in sys.path:
    sys.path.insert(0, project_home)


# set environment variable to tell django where your settings.py is
os.environ['DJANGO_SETTINGS_MODULE'] = 'wagtail_blog.settings.base'
os.environ['DJANGO_SECRET_KEY'] = 'wagtail_blog.settings.dev'



project_folder = '/home/Tecnophilosophy/tecnophilosophy_blog/'  # adjust as appropriate
url = load_dotenv(os.path.join(project_folder, '.env'))



application = get_wsgi_application()

This is the dev.py file:

import os
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv("SECRET_KEY")


# SECURITY WARNING: define the correct hosts in production!
ALLOWED_HOSTS = ['blogmain.tecnophylosophy.com']

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

In Wagtail there is no settings.py, but base.py and dev.py.

That looks like it should work. Which version of python-dotenv do you have installed? You can find out by activating your virtualenv and then running pip show python-dotenv

The version of python-dotenv is 0.15. But now I'm using another solution, that is to generate a random key with utils.get_random_secret_key() . This solution seems to be working.

Thanks for letting us know that you made it work.