我有 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 加载正常并且错误消息消失(但显然我看不到我的内容)。
我该如何解决这个问题?
编辑:继承人“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))