![](/img/trans.png)
[英]Django- site won't work unless the SECRET_KEY is hardcoded in SETTINGS.PY
[英]My Django app won't detect settings.py, and won't detect SECRET_KEY
我的 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' 模塊。
我最終做的是,我最終開始了一個全新的項目,並從舊項目復制和粘貼文件,並且它有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.