解决django项目因拆分settings.py导致的The SECRET_KEY setting must not be empty问题

一,问题描述

环境:

  1. Ubuntu 20.10
  2. python 3.8.6
  3. django 3.1.7
  4. IDE pycharmpro 2020.3

1,前期操作描述

在开发项目时将settings.py文件进行了拆分,结构如下:
解决django项目因拆分settings.py导致的The SECRET_KEY setting must not be empty问题_第1张图片
其中develop.py文件内容如下:

from .base import * # NOQA
import os

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
     
    'default': {
     
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

其中base.py文件存访除上述develop.py文件内容以外的原settings.py的内容。

在项目中manage.py与wsgi.py文件进行如下替换:

将:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'typeidea.settings')
替换为:
profile = os.environ.get('TYPEIDEA_PROFILE', 'develop')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'typeidea.settings.%s' % profile)

2,报错!

在编写好管理功能后运行项目时报错:

manage.py runserver 8000
Traceback (most recent call last):
  File "/home/dfl/py-projects/web/django/typeidea/manage.py", line 24, in <module>
    main()
  File "/home/dfl/py-projects/web/django/typeidea/manage.py", line 20, in main
    execute_from_command_line(sys.argv)
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 61, in execute
    super().execute(*args, **options)
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 68, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/dfl/py-projects/web/django/typeidea/venv/lib/python3.8/site-packages/django/conf/__init__.py", line 189, in __init__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Process finished with exit code 1

但初建项目并测试运行runserver时未报错。

二,解决方案

检查base.py,这个配置文件中是保存了除develop.py文件内容以外的原settings.py的内容的,其中SECRET_KEY不为空

考虑是base.py内容没被正确导入,但develop.py中已经进行了导入 :

from .base import *

最后考虑是环境变量出错,检查环境变量,如下:
解决django项目因拆分settings.py导致的The SECRET_KEY setting must not be empty问题_第2张图片
因为代码中已设置os.environ.setdefault(‘DJANGO_SETTINGS_MODULE’, ‘typeidea.settings.%s’ % profile),所以就在编辑中删除环境变量DJANGO_SETTINGS_MODULE,点击应用、确定,再运行开发服务器,结果成功运行:
解决django项目因拆分settings.py导致的The SECRET_KEY setting must not be empty问题_第3张图片

你可能感兴趣的:(django,django,python,web)