Django.core.exceptions.ImproperlyConfigured: SECRET_KEY parameter must not be empty

I created a new project in django and inserted some files from another project. Whenever I try to start the server, the following error message appears:

Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() . . . File "/Library/Python/2.7/site-packages/django/conf/__init__.py", line 115, in __init__ raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty. 

Here are my settings.py

 """ Django settings for dbe project. """ # Build paths inside the project like this: os.path.join(BASE_DIR, ...) import os from os.path import join as pjoin BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'shared', 'issues', 'blog', 'bombquiz', 'forum', 'portfolio', 'questionnaire', ) MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) TEMPLATE_DIRS = ( os.path.join(BASE_DIR, "templates"), os.path.join(BASE_DIR, "templates", "issues"), os.path.join(BASE_DIR, "templates", "blog"), os.path.join(BASE_DIR, "templates", "bombquiz"), os.path.join(BASE_DIR, "templates", "forum"), os.path.join(BASE_DIR, "templates", "portfolio"), os.path.join(BASE_DIR, "templates", "questionnaire"), ) ROOT_URLCONF = 'dbe.urls' WSGI_APPLICATION = 'dbe.wsgi.application' # Database # https://docs.djangoproject.com/en/1.7/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.7/howto/static-files/ MEDIA_URL = '/media/' MEDIA_ROOT = pjoin(BASE_DIR, "media") STATIC_URL = '/static/' STATICFILES_DIRS = ( pjoin(BASE_DIR, "static"), ) try: from local_settings import * except: pass 

Here also manage.py

 #!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dbe.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) 

Any help? Thanks!

+6
source share
2 answers

Like the error, you did not define SECRET_KEY . You need to add it to settings.py .

Django will refuse to start if SECRET_KEY not set.

You can learn more about this setting in the docs .

SECRET_KEY can be just about anything ... but if you want to use Django to create it, 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 SECRET_KEY to your settings file.

+15
source

SECRET_KEY variable in settings.py should be kept secret

You can put the secret key of the row generated by the get_random_string function above (as @rnevius pointed out) in settings.py, but use a function that receives the variable.

This means that for security reasons, it is better to hide the contents of the SECRET_KEY variable.

You can define an environment variable as follows:

In your $HOME/.bashrc or $HOME/.zshrc or /etc/bashrc or /etc/bash.bashrc according to your unix operating system and the terminal manager 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 file you can add this:

 import os from django.core.exceptions import ImproperlyConfigured def get_env_variable(var_name): try: 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 get_env_variable function tries to get the var_name variable from the environment, and if it does not find it, it raises the ImproperlyConfigured error. Using it this way, when you try to start the application, and the SECRET_KEY variable is not found, you can see a message about why our project is failing.

You can also protect secret variables of project contents in this way.

+3
source

Source: https://habr.com/ru/post/984698/


All Articles