我有index.html,它加载了project / p1.html,它加载了project / p2.html。 project / p1.html和project / p2.html每个加载都很好,所以我知道它们正常工作。问题是尝试将project / p1.html加载到index.html,其中包含语句:
{% include 'project/p1.html' %}
project / p1.html有声明:
{% include 'project/p2.html' %}
加载index.html,我收到错误:
VariableDoesNotExist at /
Failed lookup for key [objects] in u"[{'params': {}}, {'csrf_token': <django.utils.functional.__proxy__ object at 0x7fd3dc0e2410>}, {'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7fd3dc0e2210>, 'user': <django.utils.functional.SimpleLazyObject object at 0x7fd3dc10c790>}, {},......
如果我从project / p1.html中删除“{%include'project / p2.html'%}”,则index.html加载正常,错误消息消失(但显然我看不到我的内容)。< / p>
如何解决此问题?
编辑:继承人“project / p2.html”:
<table class="mytable">
{% for object in objects %}
<tr class="my_row">
<td>{{ object.name }}</td>
</tr>
{% endfor %}
</table>
这是views.py:
from models import Mytable
from django.shortcuts import render_to_response, RequestContext
def my_request(request,
template="project/p1.html",
page_template="project/p2.html"):
context = {
'objects': Mytable.objects.all().order_by('-date'),
'page_template': page_template,
}
if request.is_ajax():
template = page_template
return render_to_response(template, context,
context_instance=RequestContext(request))
答案 0 :(得分:5)
它正在寻找不可用的变量。传入其他上下文参数。
{% include "name_snippet.html" with person="Jane" greeting="Hello" %}
编辑:Documentation。
答案 1 :(得分:0)
在模板中包含模板没有什么特别之处,但您需要确保在模板上下文中定义了所有模板中引用的所有变量。根据您的具体操作,您可能会发现这有用:Django Templates - Changing context for an 'include' template