0

我想将 2 个列表传递给 django 模板,但只有第一个被渲染,这是 index.html:

enter code here
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
    <li>{{ poll.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

{% if menu_items_list %}
    <table id="menu_items_list">
<tr>
    {% for item_url, item_name in menu_items_list %}
    <td><a href="item_url">item_name</a>
    {% endfor %}
    </tr>
</table>
 {% endif %}
<h3>{{ index_name }}</h3>

这是urlconf:

urlpatterns = patterns('zzz.polls.views',
url(r'^$',
    ListView.as_view(
    queryset=Poll.objects.order_by('pub_date')[:5],
    context_object_name='latest_poll_list',
    template_name='index.html')),

和观点:

def index(request):
    latest_poll_list = Poll.objects.all().order_by('pub_date')[:5]
    index_name = 'INDEX PAGE'
    menu_items_list = ['somesite.com', 'Googy') for x in xrange(5)]
    return render_to_response('index.html', {'latest_poll_list': latest_poll_list,
                                             'menu_items_list' : menu_items_list,
                                             'index_name': index_name})

我在哪里犯错了??

4

1 回答 1

1

LC 的语法是错误的。

>>> ['somesite.com', 'Googy' for x in xrange(5)]
  File "<stdin>", line 1
    ['somesite.com', 'Googy' for x in xrange(5)]
                               ^
SyntaxError: invalid syntax
>>> [('somesite.com', 'Googy') for x in xrange(5)]
[('somesite.com', 'Googy'), ('somesite.com', 'Googy'), ('somesite.com', 'Googy'), ('somesite.com', 'Googy'), ('somesite.com', 'Googy')]
于 2012-08-11T06:49:23.607 回答