Django在djstripe / templates / djstripe /下搜索模板,而不是djstripe / templates /(TemplateDoesNotExist)

时间:2016-07-10 12:47:41

标签: python django django-templates

我正在使用dj-stripe库处理Django 1.9.7项目。我正在使用dj-stripe存储库的#162-api-updates-through_2015-07-28分支上的最新提交,即1f0ed7c1

尝试加载模板accounts.html时(/Users/james/venv/lib/python3.5/site-packages/djstripe/templates/djstripe/account.html),我收到TemplateDoesNotExist例外。此accounts.html模板扩展了位于djstripe/base.html的另一个模板/Users/james/venv/lib/python3.5/site-packages/djstripe/templates/base.html

Django尝试在错误的路径base.html上查找此/Users/james/venv/lib/python3.5/site-packages/djstripe/templates/djstripe/base.html模板。

我尝试将{% extends "djstripe/base.html" %}更改为{% extends "base.html" %}文件中的accounts.html,但这没有帮助。

因此,Django在djstripe/templates/djstripe/下而不是djstripe/templates/下,导致此错误。

调试错误页面上的消息是:

模板加载器postmortem

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

使用引擎django:

django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/admin/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/auth/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/djstripe/templates/base.html (Source does not exist)

...

这是我的INSTALLED_APPS设置

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

这是我的TEMPLATES设置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [],
        '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',
                'django.template.context_processors.request',
            ],
        },
    },
]

这是我的urls.py

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

urlpatterns = [
    url(r'^', include('djstripe.urls', namespace='djstripe')),
    url(r'^admin/', admin.site.urls),
]

1 个答案:

答案 0 :(得分:0)

在设置文件中,当' DIRS'保留为空,Django将仅在app_name / templates目录中查找模板,因为默认设置了' APP_DIRS':True。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': [],
        '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',
                'django.template.context_processors.request',
            ],
        },
    },
]

在您的情况下,Django正在寻找模板:

django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/admin/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/django/contrib/auth/templates/base.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Users/james/venv/lib/python3.5/site-packages/djstripe/templates/base.html (Source does not exist)

但djstripe / templates中没有base.html。 您可以在以下位置移动模板文件:

/Users/james/venv/lib/python3.5/site-packages/djstripe/templates/

或者您可以在DIRS中添加模板的路径。 这是解释: Django DIRS