我应该通过哪些变量?

时间:2012-08-28 04:39:23

标签: django django-templates

我有以下观点:

from models import Table
from django.shortcuts import render_to_response, RequestContext

def myget(request,
    template="project/p1.html",
    page_template="project/p2.html"):
    context = {
        'objects': Table.objects.all(),
        'page_template': page_template,
    }

    if request.is_ajax():
        template = page_template
    return render_to_response(template, context,
        context_instance=RequestContext(request))

我的网址文件如下:

url(r'^$', 'django.views.generic.simple.direct_to_template', {'template': 'master.html'}), 
url(r'^p1', 'project.views.myget'),

我在我的master.html文件中包含p1.html(包括p2.html),如下所示:

{% include "project/p1.html" with objects=objects %}

我在p1.html文件中包含了p2.html,如下所示:

{% include "project/p2.html" %}

我可以在我的master.html文件中看到p1.html而不是p2.html(当我自己加载p1.html时,我可以看到p2.html)。我错过了什么?除了“objects = objects”之外,我还需要加载其他变量吗?

编辑: p1.html:

<!DOCTYPE HTML>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/style/endless.js" charset="utf-8"></script>
<script type="text/javascript" src="/style/endless_on_scroll.js" charset="utf-8"></script>
</head>

<body>

{% include "project/p2.html" %}
</body>
</html>

p2.html:

{% load endless %}

<table>

{% paginate 100,100 objects %}
{% for object in objects %}
    <tr>
      <td>{{ object.name }}</td>
    </tr>
{% endfor %}
</table>


{% show_more %}

1 个答案:

答案 0 :(得分:0)

您忘记从p1.html传递对象。

{% include "project/p2.html" with objects=objects %}

另外你的urls.py还有一些东西:你使用direct_to_template而不是通常传递对象的myget视图。 'direct_to_template'不会这样做。因此,您应该创建一个传递对象的视图,而不是使用“direct_to_template”。