我的Django应用不会检测到settings.py,也不会检测到SECRET_KEY

时间:2020-03-21 14:21:54

标签: python django

我的Django应用出现问题。

由于某种原因,它不会检测到settings.py或SECRET_KEY。

首先,这是我的项目目录结构

C:.
│   manage.py
│
├───Accounts
│   │   admin.py
│   │   apps.py
│   │   models.py
│   │   tests.py
│   │   urls.py
│   │   views.py
│   │   __init__.py
│   │
│   ├───migrations
│   │       __init__.py
│   │
│   ├───templates
│   │       login.html
│   │       signup.html
│   │
│   └───__pycache__
│          omitted
│
├───Blog
│   │   admin.py
│   │   apps.py
│   │   models.py
│   │   tests.py
│   │   views.py
│   │   __init__.py
│   │
│   ├───migrations
│   │       __init__.py
│   │
│   └───templates
├───Learning
│   │   admin.py
│   │   apps.py
│   │   models.py
│   │   tests.py
│   │   views.py
│   │   __init__.py
│   │
│   ├───migrations
│   │       __init__.py
│   │
│   └───templates
├───static
│   │   compiled_sass.css
│   │   compiled_sass.css.map
│   │
│   └───sass-styles
│       │   styles.sass
│       │
│       ├───src
│       │       omitted
│       │
│       └───static
│               omitted
│
└───tf
    │   local_settings.py
    │   settings.py
    │   TODO.md
    │   urls.py
    │   wsgi.py
    │   __init__.py
    │
    ├───templates
    └───__pycache__
            omitted

现在,当我运行时,我得到django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

这就是我的settings.py的样子

"""
Django settings for tf project.

Generated by 'django-admin startproject' using Django 2.1.3.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""


import os
from ..Accounts.models import CustomUser

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'some key which is there'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'Accounts',
    'Blog',
    'Learning',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tf.urls'

AUTH_USER_MODEL = 'Techfluent.Accounts.models.CustomUser'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['tf\\templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'tf.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': '*******',
        'USER': 'postgres',
        'PASSWORD': '******',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

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/2.2/howto/static-files/
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, '..\\Techfluent\\static\\')
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '\\static\\'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL ='\\media\\'

try:
    from local_settings import *
except ImportError:
    raise Exception("A local_settings.py file is required")

你们都可以帮我吗?我在这里疯了几个小时,试图解决这个问题。

如果有帮助,我在MacOS上启动了该项目,然后将其克隆到了Windows中。是Django 2.2,Python3.7。

我显然也收到ModuleNotFoundError,因为它找不到'tf'模块。

1 个答案:

答案 0 :(得分:0)

我最终要做的是结束一个全新的项目,并从旧项目中复制并粘贴文件,并且它可以正常工作。

相关问题