Django: Improperlyconfigured: the Secret_Key Setting Must Not Be Empty

Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty

I had the same error and it turned out to be a circular dependency between a module or class loaded by the settings and the settings module itself. In my case it was a middleware class which was named in the settings which itself tried to load the settings.

Django SECRET_KEY setting must not be empty with github workflow

If you have stored the SECRET_KEY in your system's environment variable, then for GitHub workflow, you can add a dummy environment variable in the YAML file.

The settings.py should look like this

import os
...
SECRET_KEY = os.environ.get('SECRET_KEY') # Or the name by which you stored environment variable
...

The steps are given below:

Step 1: Generate a dummy SECRET_KEY. You can create it yourself by

import secrets
print(secrets.token_hex(25))

Or generate from a site like this.

Step 2: In your .github/workflows YAML file (e.g., django.yml), add this

steps:
...
- name: Run Tests
env:
SECRET_KEY: your-genereated-secret_key
run: |
python manage.py test

Then everything will work fine with the same version of code in your local environment, production environment, and GitHub workflow.

Error message: django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty

The system environment hides the original secret key of the Django app that you downloaded (Ask the owner of the code for the secret key of it or create your own django app and copy the implementation of the code). The Django secret key cannot be customized. Django generates it automatically.

Refer to question: Effects of changing Django's SECRET_KEY

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty

Just like the error says, you have no SECRET_KEY defined. You need to add one to your settings.py.

Django will refuse to start if SECRET_KEY is not set.

You can read more about this setting in the docs.

The SECRET_KEY can be just about anything...but if you want to use Django to generate one, you can do the following from the python shell:

>>> from django.utils.crypto import get_random_string
>>> chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
>>> SECRET_KEY = get_random_string(50, chars)
>>> print SECRET_KEY

Copy the SECRET_KEY to your settings file.

python-decouple | The SECRET_KEY setting must not be empty

The problem is you cannot import models before your settings are setup. So when you import the models at the top of the settings file, you get the error because the settings object hasn't been initialized, and therefore doesn't have the SECRET_KEY field set.

Ideally you wouldn't need to import models in your settings at all. But if you have to have them in there, you MAY be able to simply move them later than the SECRET_KEY, but...it's probably still going to cause problems. I'd try to refactor so you don't need them in your settings file.



Related Topics



Leave a reply



Submit