【发布时间】:2021-05-04 19:03:02
【问题描述】:
我正在尝试将我的网站发布到暂存环境,但我不断收到 TemplateDoesNotExist 错误。我的网站由多个应用程序组成,这些应用程序将为通用元素扩展基本应用程序。当我在开发环境中查看该网站时,它按预期工作,但是当我尝试在我的暂存环境中查看它时,它会出错。
应用程序中的所有模板(基本模板除外)的顶部都包含{% extends "main/index.html "%}。
文件结构:
├── manage.py
├── assets
├── base
│ ├── templates
│ │ ├── main
│ │ │ ├── index.html
│ ├── urls.py
│ ├── views.py
│ ├── ...
├── dashboard
├── inventory
│ ├── templates
│ │ ├── main
│ │ │ ├── inventory.html
│ ├── urls.py
│ ├── views.py
│ ├── ...
├── magic
│ ├── templates
│ │ ├── main
│ │ │ ├── sets.html
│ │ │ ├── cards.html
│ ├── urls.py
│ ├── views.py
│ ├── ...
├── website
│ ├── settings
│ │ ├── base.py
│ │ ├── staging.py
│ │ └── production.py
│ ├── asgi.py
│ ├── urls.py
│ └── wsgi.py
settings/base.py:
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
SECRET_KEY = ''
DEBUG = True
ALLOWED_HOSTS = [
'localhost',
'0.0.0.0',
'127.0.0.1',
]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'base.apps.BaseConfig',
'dashboard.apps.DashboardConfig',
'inventory.apps.InventoryConfig',
'magic.apps.MagicConfig',
'django_filters',
'psycopg2',
]
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 = 'website.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 = 'website.wsgi.application'
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'development',
'USER': 'superuser',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
# Password validation
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
LANGUAGE_CODE = 'en-gb'
TIME_ZONE = 'Europe/London'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets')]
settings/staging.py:
import dj_database_url
from .base import *
SECRET_KEY = os.environ.get('SECRET_KEY')
DEBUG = True
ALLOWED_HOSTS += [
'staging.herokuapp.com',
]
INSTALLED_APPS += [
'whitenoise.runserver_nostatic',
]
MIDDLEWARE += [
'whitenoise.middleware.WhiteNoiseMiddleware',
]
db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets')]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
错误:
TemplateDoesNotExist at /magic/sets/
main/index.html
Request Method: GET
Request URL: https://card-crate-admin-staging.herokuapp.com/magic/sets/
Django Version: 3.1.5
Exception Type: TemplateDoesNotExist
Exception Value:
main/index.html
Exception Location: /app/.heroku/python/lib/python3.7/site-packages/django/template/backends/django.py, line 84, in reraise
Python Executable: /app/.heroku/python/bin/python
Python Version: 3.7.9
Python Path:
['/app/.heroku/python/bin',
'/app',
'/app/.heroku/python/lib/python37.zip',
'/app/.heroku/python/lib/python3.7',
'/app/.heroku/python/lib/python3.7/lib-dynload',
'/app/.heroku/python/lib/python3.7/site-packages']
Server time: Sun, 31 Jan 2021 11:04:58 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /app/templates/main/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.7/site-packages/django/contrib/admin/templates/main/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.7/site-packages/django/contrib/auth/templates/main/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/base/templates/main/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/dashboard/templates/main/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/inventory/templates/main/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/magic/templates/main/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python3.7/site-packages/django_filters/templates/main/index.html (Source does not exist)
【问题讨论】:
-
好像忘记导入magic_app
-
抱歉导入到哪里?
-
'DIRS': os.path.join(BASE_DIR, 'templates')在项目级别将所有模板存储在templates文件夹中时有效。试试'DIRS': os.path.join(BASE_DIR, 'base', 'templates') -
添加
'DIRS': os.path.join(BASE_DIR, 'base', 'templates')会出现同样的错误 -
你的设置看起来不错,这个答案可能就是你所面临的Astraub's answer on - Django Template Not Found
标签: python python-3.x django heroku