• 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 8 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 5.3k points
0 votes
1 answer 238 views
Problem : When I try to run following command: python manage.py updatestats I get the error as below: “TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'” I am unable to figure out ... ) File "/Users/alecxe/project/Dev/project_files/project/main/management/commands/updatestats.py", line 34, in handle unique_visits=display_unique_visits(hostname),)
asked Jan 2, 2020 alecxe 7.5k points
0 votes
1 answer 1.9K views
Problem : I am facing the issue in passing a function as a parameter to another function as follows typeerror: int() argument must be a string, a bytes-like object or a number, not 'nonetype'
asked Nov 18, 2019 peterlaw 6.9k points
1 vote
1 answer 79 views
Problem: I have fundamental knowledge of Django. I have learned Django with the help of online tutorials. Recently I have started working on Django. I have recently tried to upgrade my Django to the Django 1.10. Since then I am often facing below error: TypeError: ... ), ] I have only fundamental knowledge about Django so unable to fix the above error. Kindly help me in fixing above Django error.
asked Jun 8, 2020 Martin K 6.6k points
0 votes
2 answers 322 views
Problem : After upgrading it to Django 1.10, I get below error: “TypeError: view must be a callable or a list/tuple in the case of include().” My urls.py is as below : from django.conf.urls import include, url urlpatterns = [     url(r'^$', 'myapp.views.home' ... r'^contact/$', 'myapp.views.contact'),     url(r'^login/$', 'django.contrib.auth.views.login'), ] How can I fix above error?
asked Jan 23, 2020 jwilliam 3.9k points
0 votes
1 answer 256 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 2.2K views
Problem : I want to deploy the Django application to a web, but I face the below error: “You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path”
asked Feb 3, 2020 jwilliam 3.9k points
0 votes
1 answer 16 views
0 votes
1 answer 12 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 17 views
Problem: I am trying to implement Social Login in Django. I have already installed social_auth_app_django with below command pip install social-auth-app-django And I also added this to installed apps in my settings file. INSTALLED_APPS = [ 'django.contrib.admin', ' ... migration python manage.py migrate ModuleNotFoundError : No module named mysite.core Note. I am using Python 3.6.2andDjango 1.11.7
asked Dec 25, 2020 sasha 5.3k points