【问题标题】:Template does not exist after splitting the settings in Django在Django中拆分设置后模板不存在
【发布时间】:2021-08-12 14:08:08
【问题描述】:

这是我的回溯错误: .

在我拆分设置(开发、生产)之前,一切都运行良好。现在我遇到了模板不存在的问题。这是我的设置文件:

base_settings.py:

import os
from pathlib import Path

BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), 
'../../'))

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'autoslug',
'accounts',
'stellar',
'stellar_sdk',
]

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 = 'stellarLedger.urls'

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': os.path.join(BASE_DIR, '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 = 'stellarLedger.wsgi.application'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email username'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
LOGOUT_REDIRECT_URL = '/'
AUTH_USER_MODEL = "accounts.customUser"

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

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

# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/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/3.2/howto/static-files/

STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
 os.path.join(BASE_DIR, 'static'),
)
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

development_settings.py

from .base_settings import *

SECRET_KEY = 'secret key'

DEBUG = True

ALLOWED_HOSTS = ['192.168.0.102', '127.0.0.1']

production_settings.py

from .base_settings import *

DEBUG = False

ALLOWED_HOSTS = ['mysite.com', 'ip.of.my.site', 'www.mysite.com', ]

我在这里做错了什么?未找到的模板位于模板根目录中。

这是我的项目结构:

  • 项目/
    • 项目
      • 初始化.py
      • 设置
        • 初始化.py
        • base_settings.py
        • development_settings.py
        • production_settings.py
      • wsgi.py
      • asgi.py
    • 帐户
      • 模板
        • 帐户
          • test.html
    • 模板
      • base.html
      • index.html
    • 静态

【问题讨论】:

  • 您能打印BASE_DIR 看看它是否指向正确的目录吗?
  • BASE_DIR 为项目根目录
  • 我知道。只需print(BASE_DIR) 并检查它是否指向正确的路径。您需要彻底检查您的项目目录结构。
  • 它正在打印这个/home/nasir/Documents/stellarLedger。 stellarLedger 是我的项目名称。它应该打印什么?
  • 能否发一张项目目录结构的图片。

标签: python django


【解决方案1】:

您更改了默认名称 settings.py。还原 settings.py 的所有更改并在 DEBUG = True 之后添加此行:

productions_hosts = ['mysite.com', 'ip.of.my.site', 'www.mysite.com']
development_hosts = ['192.168.0.102', '127.0.0.1']

ALLOWED_HOSTS = development_hosts if DEBUG else productions_hosts

还有一件事,保守秘密!

【讨论】:

    猜你喜欢
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 2017-06-15
    • 2021-04-16
    • 1970-01-01
    • 2017-01-23
    • 2010-12-27
    相关资源
    最近更新 更多