将2个或更多变量传递给django模板

时间:2012-08-11 06:45:32

标签: python django-templates

我想将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})

我犯了错误?

1 个答案:

答案 0 :(得分: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')]