TemplateDoesNotExist at /base/index.html 部署到heroku时

Posted

技术标签:

【中文标题】TemplateDoesNotExist at /base/index.html 部署到heroku时【英文标题】:TemplateDoesNotExist at / base/index.html when deploying to heroku 【发布时间】:2016-12-07 02:48:46 【问题描述】:

我已经研究这个问题好几天了,我真的很想在这里得到一些真正的帮助! :)

我是 Python、Django 和 *** 的新手,所以请告诉我更多信息或其他格式是否会有所帮助。

我正在尝试在 Heroku 上部署我的应用程序。它在本地运行,但是当我尝试

heroku 打开

我收到以下错误:

模板不存在于 / 基础/index.html

我已经看到其他人发生这种情况,并且我尝试了以下没有奏效的修复:

通过将 index.html 直接放入模板而不是模板来更改文件夹结构>base>index.html (TemplateDoesNotExist when base template is in root folder) - 这会导致应用程序停止在本地运行 通过添加 将模板添加为已安装的应用程序(就像我为核心所做的那样)

(r'', 包括('templates.urls')),

到我的 urls.py

并添加了

'模板'

到我的 settings.py。这导致应用程序停止在本地运行,因此我将其更改回来。

将我的 views.py 更改为:
from django.shortcuts import render from django.views.generic.base
import TemplateView
# Create your views here. class LandingView(TemplateView):
   template_name = "base/index.html"

from django.shortcuts import render
from django.views.generic.base import TemplateView
# Create your views here.
class LandingView(TemplateView):  
    template_name = [os.path.join(MAIN_DIR, 'coffeedapp2/templates')],
检查以确保我有标准的 Django 1.8x 模板设置 (TemplateDoesNotExist at / base/index.html) 尝试将我的 views.py 更改为:
from django.shortcuts import render
from django.views.generic.base import TemplateView
# Create your views here.
class LandingView(TemplateView):
  template_name = "base/index.html"

from django.shortcuts import render
from django.views.generic.base import TemplateView
# Create your views here.
class LandingView(TemplateView):
  template_name = "base/index.html"

(TemplateDoesNotExist at / at templates/index.html)

我在想我可能需要定义一个 SITE_ROOT 或其他东西,但每当我尝试这样做时,它就会停止在本地运行。

错误:

TemplateDoesNotExist at /base/index.html 请求方法:GET请求 网址:https://salty-journey-18003.herokuapp.com/ Django 版本:1.9.7 异常类型:TemplateDoesNotExist 异常值:base/index.html 例外 位置:/app/.heroku/python/lib/python2.7/site-packages/django/template/loader.py 在 select_template,第 74 行 Python 可执行文件:/app/.heroku/python/bin/python Python版本:2.7.12 Python 路径:['/app', '/app/.heroku/python/bin', '/app/.heroku/python/lib/python2.7/site-packages/setuptools-23.1.0-py2.7.egg', '/app/.heroku/python/lib/python2.7/site-packages/pip-8.1.2-py2.7.egg', '/app', '/app/.heroku/python/lib/python27.zip', '/app/.heroku/python/lib/python2.7', '/app/.heroku/python/lib/python2.7/plat-linux2', '/app/.heroku/python/lib/python2.7/lib-tk', '/app/.heroku/python/lib/python2.7/lib-old', '/app/.heroku/python/lib/python2.7/lib-dynload', '/app/.heroku/python/lib/python2.7/site-packages'] 服务器时间:周一,1 2016 年 8 月 20:50:53 +0000 模板加载器事后分析

Django 尝试按以下顺序加载这些模板:

使用引擎django:django.template.loaders.filesystem.Loader: /coffedapp2/templates/base/index.html(源不存在) django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/templates/base/index.html (来源不存在) django.template.loaders.app_directories.Loader: /app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/templates/base/index.html (来源不存在)

settings.py

"""

Django settings for coffeedapp2 project.

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

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

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

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MAIN_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))



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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '^h)ohz4qbhu&5po084_ob8qy+1c*h^tb#jtab!p965^8@&64q!'

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

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]

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

ROOT_URLCONF = 'coffeedapp2.urls'

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(MAIN_DIR, 'coffeedapp2/templates')],
        'APP_DIRS': True,
        'OPTIONS': 
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        ,
    ,
]

WSGI_APPLICATION = 'coffeedapp2.wsgi.application'


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

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


# Update database configuration with $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'] = dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']


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

STATIC_URL = '/static/'


STATICFILES_DIRS = (
    os.path.join(MAIN_DIR, 'coffeedapp2', 'static'),
    )

coffeeapp>urls.py

from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from django.conf.urls import patterns

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    (r'', include('core.urls')),
)

核心>urls.py

from django.conf.urls import patterns, include, url
import core.views as coreviews
from django.conf.urls import include

urlpatterns = patterns('',

    url(r'^$', coreviews.LandingView.as_view()),
 )

核心>views.py

from django.shortcuts import render
from django.views.generic.base import TemplateView
# Create your views here.
class LandingView(TemplateView):
    template_name = "base/index.html"

See image of file structure

Relevant file structure:

coffeedapp2
  coffeedapp2
    _init_.py
    settings.py
    settings.py
    urls.py
    wsgi.py
  core
    migrations
    _init_.py
    admin.py
    apps.py
    models.py
    tests.py
    urls.py
    views.py
  static
  templates
    base
        index.html

【问题讨论】:

【参考方案1】:

在您的TEMPLATES 设置中,尝试将您的DIRS 设置更改为:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

这是通常的方法,我看不出有任何理由像你现在那样使用MAIN_DIR

保持模板为

template_name = "base/index.html"

【讨论】:

非常感谢!!!那行得通!在 Heroku 上没有 CSS 的情况下它会通过,但我可能会弄清楚如何解决这个问题。非常感谢!!!!【参考方案2】:

我遇到了这个问题,因为我在我的 HTML 模板名称中使用了驼峰式,但是用小写字母来调用它。将模板名称更改为小写,并且有效。

【讨论】:

【参考方案3】:

我也有这个问题。问题是我有:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

在我的 settings.py 中,而不是:

'DIRS': [os.path.join(BASE_DIR, 'Templates')],

模板目录有一个大写的 T,但我在 settings.py 中输入了一个小写的 t。

【讨论】:

以上是关于TemplateDoesNotExist at /base/index.html 部署到heroku时的主要内容,如果未能解决你的问题,请参考以下文章

Django出错提示TemplateDoesNotExist at /

TemplateDoesNotExist at /polls/ - 在 Django 教程中

python Django问题:TemplateDoesNotExist at /index/

Django:无法让主页正确显示 TemplateDoesNotExist at /

Django 管理员 TemplateSyntaxError / TemplateDoesNotExist at /admin/

TemplateDoesNotExist at /base/index.html 部署到heroku时