Django“TemplateDoesNotExist"错误但“使用加载程序 django.template.loaders.app_directories.Loader"文件已存在 [英] Django "TemplateDoesNotExist " Error but "Using loader django.template.loaders.app_directories.Loader" File Exists
问题描述
Template Loader 找到模板但模板没有加载
TemplateDoesNotExist at/cardpayment/cardpayment.html请求方式:GET请求地址:http://localhost:7000/cardpayment/Django 版本:1.8异常类型:TemplateDoesNotExist异常值:cardpayment.html异常位置:/home/sdr/sl/lib/python3.4/site-packages/django/template/loader.py in render_to_string, line 138Python 可执行文件:/home/sdr/sl/bin/pythonPython版本:3.4.3蟒蛇路径:['/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']服务器时间:2015 年 5 月 5 日,星期二 10:17:40 +0000模板加载器事后分析Django 尝试按以下顺序加载这些模板:使用加载器 django.template.loaders.filesystem.Loader:/home/sdr/sl/agryp/templates/cardpayment.html(文件不存在)使用加载器 django.template.loaders.app_directories.Loader:/home/sdr/sl/agryp/agryp/templates/cardpayment.html(文件存在)<========== 文件存在但未加载/home/sdr/sl/src/tastypie/tastypie/templates/cardpayment.html(文件不存在)/home/sdr/sl/lib/python3.4/site-packages/grappelli/templates/cardpayment.html(文件不存在)/home/sdr/sl/lib/python3.4/site-packages/django/contrib/admin/templates/cardpayment.html(文件不存在)/home/sdr/sl/lib/python3.4/site-packages/django/contrib/auth/templates/cardpayment.html(文件不存在)/home/sdr/sl/lib/python3.4/site-packages/oauth2_provider/templates/cardpayment.html(文件不存在)/home/sdr/sl/lib/python3.4/site-packages/selectable/templates/cardpayment.html(文件不存在)
可以清楚地看到,加载程序能够找到模板.
settings.py 中的 TEMPLATE_DIRS 值如下:
TEMPLATES = [{'后端':'django.template.backends.django.DjangoTemplates','DIRS': [ os.path.join(BASE_DIR, "templates"),],'APP_DIRS':是的,'选项': {上下文处理器":['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 的内容
{% 扩展 "base.html" %}{% block title %}电话卡支付{% endblock %}{% 阻止额外的头 %}{% 加载 selectable_tags %}{% include_ui_theme %}{% 结束块 %}{% 块内容 %}<h1>接收卡支付</h1><form name="paymentType" id="paymentType" class="form-horizontal"><字段集><label>检查客户类型<input type="radio" value="existing">现有客户<br/><input type="radio" value="new">Nee Customer<br/>标签></fieldset></表单><div class="row"><form class="form-horizontal"><table class="table-responsive table-bordered">{{ form.as_table }}</表单>
{% 结束块 %}
我遇到了同样的问题,有效的解决方案是在模板设置中指定我的模板目录(项目/模板),如下所示:
>
TEMPLATES = [{'后端':'django.template.backends.django.DjangoTemplates','目录':['模板'],'APP_DIRS':是的,'选项': {上下文处理器":['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",],},},
]
Template Loader finds the template but template is not loaded
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)
As it can be clearly seen, the loader is able to find the template.
The TEMPLATE_DIRS value in settings.py is as follows:
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',
],
},
},
]
I have tried to move the template to project/templates directory as well but the error persists.
Code checks out with 0 errors/warnings.
contents of 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 %}
i've been running into the same problem, the solution that work was to specify my template directory (projects/templates) in templates settings like this:
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",
],
},
},
]
这篇关于Django“TemplateDoesNotExist"错误但“使用加载程序 django.template.loaders.app_directories.Loader"文件已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!