Forums

No recent changes to settings, getting SecretKey cannot be empty

Hi,

I've been doing some coding on a site using Django for a few days. I've been using a settings_local.py file to store the secret key, as well as the database connection info etc. as I use different values on my development computer.

When I pulled my last commit, which made minor changes to a js and a html file, the site started giving me 500 internal server error on some of my views. Checking the error logs it gives me the error message mentioned in the title;

2016-01-28 09:10:48,374 :django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Considering how I did not make any changes to a .py file when the error started occuring I am unsure how to proceed. I've googled, but I've been unable to find a cause. While I am certain I could use other solutions I've seen such as using environment variables, I would rather make my current approach work again if possible. I'm also not sure if it started at the exact moment I performed the update, but I haven't gotten the error previously.

Does anyone have any ideas as to why it suddenly doesn't load the secret key? It still works on my local machine with the exact same settings.py and settings_local.py containing the secret key.

Hm. What happens if you do a

python manage.py shell
from django.conf import settings
print(settings.SECRET_KEY)

?

It prints the secret key that is listed in my settings_local.py file.

OK, so it works in a bash console.

Is there anything in your WSGI file that would somehow mess with which settings are being loaded?

I haven't modified my wsgi file, so I would assume not. Upon comparing it with another sites wsgi file I see no difference.

The only settings related lines I can see are

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

Which as far as I know are correct.

OK. And I'm assuming GamePage.settings always imports from settings_local.py?

Just to double-check, have you reloaded your web app?

Yes it does, this is the exact code I use to import it if that matters.

try:
    from .settings_local import *
except ImportError:
    pass

And yes, I've tried to reload my web app. I tried again just now, just in case. Still receive the error.

What else could be different between the environment the webapp is running and the one you got at the command line? Are you using a virtualenv? Is it definitely the same Python version?

One thing you could do is remove the try/except from the import. If settings_local is definitely there, then you don't need it, right?