b-r*_*rat 4 python django django-templates
Template 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)
Run Code Online (Sandbox Code Playgroud)
可以清楚地看到,加载器能够找到模板.
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',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
我也尝试将模板移动到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 %}
Run Code Online (Sandbox Code Playgroud)
小智 6
我一直遇到同样的问题,工作的解决方案是在模板设置中指定我的模板目录(项目/模板),如下所示:
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",
],
},
},
Run Code Online (Sandbox Code Playgroud)
]
TemplateDoesNotExist... cardpayment.html可能意味着 Django can't find cardpayment.html,或者它可能意味着它可以找到 cardpayment.html 没问题,但无法在其中找到一些 {% include 'cardpayment_subsection.html' %}。
解释:
我刚刚在一个已经工作多年的项目中遇到了这个错误,这里的其他解决方案对我没有帮助。
我cardpayment.html 被模板加载器找到,但包含一些废话,这意味着它无法呈现。错误消息误导我认为 Django 不知道该文件存在,而实际上它知道它存在,只是无法成功呈现它。
有一段时间一切正常:cardpayment.html渲染没有问题。
视图.py
def cardpaymentview(request):
return render_to_response("cardpayment.html", {
"message": "Hi SO"
}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
突然,即使我没有编辑cardpayment.html,我也收到了这个错误:
TemplateDoesNotExist at /cardpaymentpage: cardpayment.html
Using loader django.template.loaders.app_directories.Loader:
folder/lib/python2.7/site-packages/django/contrib/admin/templates/cardpayment.html (File does not exist)
folder/project/app1/templates/cardpayment.html (File does not exist)
folder/project/app2/templates/cardpayment.html (File does not exist)
folder/project/paymentapp/templates/cardpayment.html (File exists)
Run Code Online (Sandbox Code Playgroud)
问题是我cardpayment.html又include调用了另一个模板:
{% include 'cardpayment_subsection.html' %}
Run Code Online (Sandbox Code Playgroud)
我通过将文件重命名为而不进行编辑cardpayment_subsection.html来创建错误,因此该命令自然失败。something_else.htmlcardpayment.htmlinclude
但是正如您所看到的,调试消息并没有表明问题是调用cardpayment_subsection.html(文件不再存在)。
总结:如果其他解决方案对您不起作用,请尝试创建一个cardpayment2.html简单地说“Hello world”的测试文件,看看您是否至少可以渲染它。也许cardpayment.html正在读取文件,但其中有错误。(就我而言,我需要将 更改{% include '____.html' %}为引用实际存在的文件!)
找到了修复方法。我不知道为什么,但新的 TEMPLATES 指令(Django 1.8+)似乎不起作用。我将其注释掉并放入旧样式
TEMPLATE_DIRS = (
'/home/sdr/sl/agryp/templates/',
)
Run Code Online (Sandbox Code Playgroud)
指令,一切又开始运转。啊啊!
需要弄清楚为什么新的 TEMPLATES 指令给我带来了问题。
感谢您尝试帮助大家!
| 归档时间: |
|
| 查看次数: |
16123 次 |
| 最近记录: |