Django“TemplateDoesNotExist”错误,但是“使用加载器django.template.loaders.app_directories.Loader”文件已存在 [英] Django "TemplateDoesNotExist " Error but "Using loader django.template.loaders.app_directories.Loader" File Exists
问题描述
模板加载程序查找模板,但模板未加载
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.
settings.py中的TEMPLATE_DIRS值如下所示:
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',
],
},
},
]
我已经尝试将模板移动到project / templates目录,但错误仍然存在。
I have tried to move the template to project/templates directory as well but the error persists.
代码检查出0错误/警告。
Code checks out with 0 errors/warnings.
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 %}
推荐答案
m也使用与您相似的方式的 DIRS
配置选项,我刚刚遇到与Django 1.8.2相同的问题。这个问题似乎与您调用 django.shortcuts.render
(或 django.shortcuts.render_to_response
)的方式有关您的意见 - 您是否使用这些?
I'm also using the DIRS
config option in a similar way to you and I've just run into the same problem in Django 1.8.2. The issue seems to relate to the way you invoke django.shortcuts.render
(or django.shortcuts.render_to_response
) in your views - are you using these?
TL; DR - 尝试在您的视图中更改您对 render_to_response
的调用您不会通过 context_instance
或任何不推荐使用的参数,例如以前经常使用的 django.template.RequestContext
。如果您通过 context_instance
或任何已弃用的参数,则django模板加载代码将使用不支持 DIRS
选项,您的模板将不会加载 - 即使它存在于磁盘上。
TL;DR - try changing your invocation of render_to_response
in your views so that you don't pass a context_instance
or any deprecated argument, such as the previously often used django.template.RequestContext
. If you do pass a context_instance
, or any deprecated argument, the django template loading code uses a legacy path that doesn't support the DIRS
option and your template will not be loaded - even if it exists on disk.
以下是Django源代码中的一段相关代码( django / shortcuts.py
),显示了这种情况如何:
Here's a section of relevant code from the Django source (django/shortcuts.py
) that shows how this situation comes about:
def render_to_response(template_name, context=None,
context_instance=_context_instance_undefined,
content_type=None, status=None, dirs=_dirs_undefined,
dictionary=_dictionary_undefined, using=None):
"""
Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
if (context_instance is _context_instance_undefined
and dirs is _dirs_undefined
and dictionary is _dictionary_undefined):
# No deprecated arguments were passed - use the new code path
content = loader.render_to_string(template_name, context, using=using)
else:
# Some deprecated arguments were passed - use the legacy code path
content = loader.render_to_string(
template_name, context, context_instance, dirs, dictionary,
using=using)
return HttpResponse(content, content_type, status)
如果你可以通过这个加载器的 render_to_string
方法(在 django / template / loader.py
中),你可以看到你的模板如果您在视图中将任何不推荐使用的参数传递到 render_to_reponse
,则不会加载:
If you follow this through to the loader's render_to_string
method (in django/template/loader.py
) you can see your template won't be loaded if you pass any deprecated arguments to render_to_reponse
in your views:
def render_to_string(template_name, context=None,
context_instance=_context_instance_undefined,
dirs=_dirs_undefined,
dictionary=_dictionary_undefined,
request=None, using=None):
"""
Loads a template and renders it with a context. Returns a string.
template_name may be a string or a list of strings.
"""
if (context_instance is _context_instance_undefined
and dirs is _dirs_undefined
and dictionary is _dictionary_undefined):
# No deprecated arguments were passed - use the new code path
if isinstance(template_name, (list, tuple)):
template = select_template(template_name, using=using)
else:
template = get_template(template_name, using=using)
return template.render(context, request)
else:
# Some deprecated arguments were passed - use the legacy code path
for engine in _engine_list(using):
try:
# This is required for deprecating properly arguments specific
# to Django templates. Remove Engine.render_to_string() at the
# same time as this code path in Django 2.0.
if isinstance(engine, DjangoTemplates):
if request is not None:
raise ValueError(
"render_to_string doesn't support the request argument "
"when some deprecated arguments are passed.")
continue
# Hack -- use the internal Engine instance of DjangoTemplates.
return engine.engine.render_to_string(
template_name, context, context_instance, dirs, dictionary)
elif context_instance is not _context_instance_undefined:
warnings.warn(
"Skipping template backend %s because its render_to_string "
"method doesn't support the context_instance argument." %
engine.name, stacklevel=2)
elif dirs is not _dirs_undefined:
warnings.warn(
"Skipping template backend %s because its render_to_string "
"method doesn't support the dirs argument." % engine.name,
stacklevel=2)
elif dictionary is not _dictionary_undefined:
warnings.warn(
"Skipping template backend %s because its render_to_string "
"method doesn't support the dictionary argument." %
engine.name, stacklevel=2)
except TemplateDoesNotExist:
continue
if template_name:
if isinstance(template_name, (list, tuple)):
template_name = ', '.join(template_name)
raise TemplateDoesNotExist(template_name)
else:
raise TemplateDoesNotExist("No template names provided")
这篇关于Django“TemplateDoesNotExist”错误,但是“使用加载器django.template.loaders.app_directories.Loader”文件已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!