Forums

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

Hello, for the past couple of weeks I've trying to get my Wagtail project going on PythonAnywhere.

I've set up a secret key environmental variable but I'm still getting this error in the logs after reloading and opening the web app, even though the app works fine locally.

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

If I don't run source .env before the local server (see below), I get the secret key error.

$ source .env
$ python manage.py runserver

This all leads me to believe it must be an environment problem. Have tried doing source .env in the PA bash console as well, doesn't change anything.

I've referred to this topic but no luck.

Is there any light you can shed on this? Thanks!

See this help page

Thanks, I've already tried this — it even echoes the secret key back to me, but still getting the error. I'd appreciate it if someone from PA could look at my code.

Are you using the environment variable to set the SECRET_KEY setting in your Django settings?

Wagtail has the settings saved in a base.py file. I've changed my wsgi file to import Django settings this way:

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings.base'

instead of (for Django only):

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

And it works :)

ah! good to know!

Actually, I found out the correct settings are depending on the environment: one would have to load ".dev" for development and ".production" for production.

# Development
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings.dev'
# Production
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings.production'