在学习Django book中,看到书数据酷这一章节中的案例,views不能没有问题,但书中没有提供template部分的代码自己尝试着补全的时候,发现如果希望页面出现:第N个问题是:xxx。这个效果的时候,xxx部分没有问题,但是N这个数值怎样也出不来,想请教一下怎样在模版中迭代出这个数值。尝试了一下enumerate,发现模版中不支持。具体代码如下:
# views.polls_list:
def polls_list(request):
    db = MySQLdb.connect(user='gaaa', db='polls', passwd='xxx', 
                               host='localhost') 
    cursor = db.cursor()  
    cursor.execute('SELECT question FROM polls_poll ORDER BY 
                         pub_date') 
    questions = [row[0] for row in cursor.fetchall()] 
    db.close()  
    return render_to_response('poll_list.html',locals())
# templates.poll_list.html
{% extends "base.html" %}
{% block title %}polls list{% endblock %}
{% block content %}
<ul>
    {% for question in questions %}
            <li>{{question}}</li>  #这个循环可以正常显示
    {% endfor %}
</ul>
{% endblock %}