首页 > 解决方案 > Heroku生产环境中的Django TemplateDoesNotExist错误但在本地开发环境中没有

问题描述

我正在尝试将我的网站发布到暂存环境,但我不断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

设置/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')]

设置/暂存.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)

标签: pythonpython-3.xdjangoheroku

解决方案


我弄清楚了我的问题,并觉得自己像个白痴。我的问题是我有:

{% extends "main/index.html " %}

代替

{% extends "main/index.html" %}


推荐阅读