Django“TemplateDoesNotExist”错误但“使用加载程序 django.template.loaders.app_directories.Loader”文件存在

新手上路,请多包涵

模板加载器找到模板但未加载模板

TemplateDoesNotExist at /cardpayment/

cardpayment.html

Request Method:     GET
Request URL:    http://localhost:7000/cardpayment/
Django Version:     1.8
Exception Type:     TemplateDoesNotExist
Exception Value:

cardpayment.html

Exception Location:     /home/sdr/sl/lib/python3.4/site-packages/django/template/loader.py in render_to_string, line 138
Python Executable:  /home/sdr/sl/bin/python
Python Version:     3.4.3
Python Path:

['/home/sdr/sl/agryp',
 '/home/sdr/pycharm-4.0.6/helpers/pydev',
 '/home/sdr/sl/src/tastypie',
 '/home/sdr/sl/agryp',
 '/usr/local/lib/python34.zip',
 '/usr/local/lib/python3.4',
 '/usr/local/lib/python3.4/plat-linux',
 '/usr/local/lib/python3.4/lib-dynload',
 '/home/sdr/sl/lib/python3.4/site-packages']

Server time:    Tue, 5 May 2015 10:17:40 +0000
Template-loader postmortem

Django tried loading these templates, in this order:

    Using loader django.template.loaders.filesystem.Loader:
        /home/sdr/sl/agryp/templates/cardpayment.html (File does not exist)
    Using loader django.template.loaders.app_directories.Loader:
        /home/sdr/sl/agryp/agryp/templates/cardpayment.html (File exists) <=========== FILE EXISTS BUT NOT LOADED
        /home/sdr/sl/src/tastypie/tastypie/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/grappelli/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/django/contrib/admin/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/django/contrib/auth/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/oauth2_provider/templates/cardpayment.html (File does not exist)
        /home/sdr/sl/lib/python3.4/site-packages/selectable/templates/cardpayment.html (File does not exist)

可以清楚地看到,加载程序能够找到模板。

settings.py中的TEMPLATE_DIRS值如下:

 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',
                'allauth.account.context_processors.account',
                'allauth.socialaccount.context_processors.socialaccount',
            ],
        },
    },
]

我也尝试将模板移动到 project/templates 目录,但错误仍然存在。

代码检出 0 个错误/警告。

cardpayment.html 的内容

{% extends "base.html" %}
{% block title %}Card Payments over Phone{% endblock %}
{% block extrahead %}
    {% load selectable_tags %}
    {% include_ui_theme %}
{% endblock %}

{% block content %}
    <h1>Receive Card Payment</h1>
    <form name="paymentType"  id="paymentType" class="form-horizontal">
    <fieldset>
        <label>Check type of Customer
        <input type="radio" value="existing">Existing Customer<br />
        <input type="radio"  value="new">Nee Customer<br />
        </label>
    </fieldset>
    </form>

    <div class="row">
    <form class="form-horizontal">
        <table class="table-responsive table-bordered">
            {{ form.as_table }}
        </table>
    </form>
    </div>
{% endblock %}

原文由 b-rat 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 547
1 个回答

我遇到了同样的问题,有效的解决方案是在模板设置中指定我的模板目录(项目/模板),如下所示:

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

]

原文由 Ours 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏