• Register
0 votes

Problem :

I want to set up the multiple setting files (e.g. Dev, Prod, ..) that include some base settings also. But unable to gain successes in my goal.

Whenever I am trying to run my ./manage.py runserver I am facing the following error:

(cb)alecxe@den /srv/www/cb $ ./manage.py runserver
ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Below is my settings module:

(cb)alecxe@den /srv/www/cb/cb/settings $ ll
total 24
-rw-rw-r--. 1 alecxe alecxe 8230 Dec  2 02:56 base.py
-rw-rw-r--. 1 alecxe alecxe  489 Dec  2 03:09 development.py
-rw-rw-r--. 1 alecxe alecxe   24 Dec  2 02:34 __init__.py
-rw-rw-r--. 1 alecxe alecxe  471 Dec  2 02:51 production.py


Code in my manage.py as below:

(cb) alecxe @den /srv/www/cb $ cat manage.py 
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cb.settings.development")
from django.core.management import execute_from_command_line
6 5 3
7,540 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

On my Mac OS I did not like that it did not find the env variable set in my settings file:

# SECURITY WARNING: keep the secret key used in production secret!

But when I added the env var to my local Mac OS dev env as follows, the error just disappeared:

export MY_SERVER_ENV_VAR_NAME ='fake dev security key that is longer than 50 characters.'

In my case, I also had to add the --settings param:

python3 manage.py check --deploy --settings myappname.settings.production

Here the production.py is a file containing production specific settings inside my settings folder.

9 7 4
38,600 points
0 votes



You need to add one to your settings.py.

Django will refuse to start if SECRET_KEY is not set.

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.

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.

In your $HOME/.bashrc or $HOME/.zshrc or /etc/bashrc or /etc/bash.bashrc according to your unix operating system and terminal manager that you use:

export SECRET_KEY='value_of_the_secret_key_generated_by_get_random_string_function'

you can look something like this:

export SECRET_KEY='lmrffsgfhrilklg-za7#57vi!zr)ps8)2anyona25###dl)s-#s=7=vn_'


And in the settings.py you can add this:

import os
from django.core.exceptions import ImproperlyConfigured

def get_env_variable(var_name):
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)

SECRET_KEY = get_env_variable('SECRET_KEY')

The function get_env_variable tries to get the variable var_name from the environment, and if it doesn’t find it, it raises an ImproperlyConfigured error. Using it in this way, when you try to run your app and the SECRET_KEY variable is not found, you will be able to see a message indicating why our project fails.

10 6 4
31,120 points

Related questions

0 votes
1 answer 3 views
Problem: Why I got the error > Django.core.exceptions.improperlyconfigured: error loading mysqldb module.
asked 4 days ago ummeshani 8.8k points
0 votes
1 answer 90 views
Problem: I got stuck in the process when I wanted to deploy a django project to the server today. When I run python manage.py runserveron the server, the terminal shows me this: Traceback (most recent call last): File "manage.py", ... "dict.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) Can anyone give me some advice? Thank you very much
asked Dec 7, 2020 sasha 16.2k points
1 vote
1 answer 50 views
Problem: tried to import reverse from django.core.urlresolvers. Using Python 3.5.2, Django 2.0 and MySQL. modulenotfounderror: no module named 'django.core.urlresolvers'
asked Feb 24 Sofia Castro 4.2k points
0 votes
1 answer 378 views
Problem : I am not very sure how can I fix the below issue I have really no idea about why am I facing below error while trying to do the runserver: Performing system checks... System check identified no issues (0 silenced). Unhandled exception in thread ... server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
asked Dec 25, 2019 alecxe 7.5k points
0 votes
1 answer 4 views
Problem: I got error on my django rest framework, I am running it on windows 10 OS. this is the entire error: Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "c:\django\django\django\core\ ... api-auth/', include('rest_framework.urls', namespace='rest_framework')) ] does anyone have an idea about my situation? thanks in advance...
asked 5 days ago Yeamin 21.4k points
1 vote
1 answer 29 views
Problem : I am quite new in django...I trying to access the admin page.it gives me the following error: AttributeError: 'WSGIRequest' object has no attribute 'user'I already googled about this problem but still I cannot solve it for my case in any way. Can you help ?
asked Mar 6 Sabbir Hossain 14.6k points
0 votes
1 answer 13 views
I downloaded python 3.8.6 on windows 10 and need some guidance on how to install Django
asked Oct 1, 2020 turpotu 140 points
0 votes
1 answer 26 views
Problem: Is it normal to be happen ? how can I fix it : You're using the staticfiles app without having set the static_root setting to a filesystem path..
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 3.5K views
Problem : I want to deploy the Django application to a web, but I face the below error: &ldquo;You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path&rdquo;
asked Feb 3, 2020 jwilliam 3.9k points
1 vote
1 answer 5 views
Problem: Any one here to figure out thids problem? Typeerror: super() argument 1 must be type, not classobj.
asked Apr 26 PkGuy 23.5k points