Django:循环调用template + view

问题描述

| 我正在进行多页民意调查。我有一个数据库,该数据库一个称为页面的列,每个页面有4个问题。用户页面上回答所有问题后,单击“下一步”并转到下一页。 我可以查询数据库以找出需要生成页面数,但是我不确定如何循环模板渲染过程。 这是我的代码用户输入:
def index(request):
latest_poll_list = Poll.objects.filter(page=1)
t = loader.get_template(\'index.html\')
c = Context({
    \'latest_poll_list\': latest_poll_list,})
return HttpResponse(t.render(c))
...将生成此模板:
    <ul>

<form action=\"/first/Vote/\" method=\"post\">
{% csrf_token %}
{% for poll in latest_poll_list %}
    <li>{{ poll.question }}</li>
        {% for choice in poll.choice_set.all %}
            <input type=\"radio\" name=\"choice{{ poll.id }}\" id=\"{{poll.id}}choice{{ forloop.counter }}\" value=\"{{ choice.id }}\" />
            <label for=\"choice{{ forloop.counter }}\">{{ choice.choice }}</label><br />
        {% endfor %}
{% endfor %}
    <input type=\"submit\" value=\"Vote\" />
</form>

</ul>
...然后在这里处理用户的选择:
def Vote(request):
intra_page_key = request.POST[\'choice1\']+\',\'+request.POST[\'choice2\']+\',\'+request.POST[\'choice3\']
request.session[\'p1\'] = intra_page_key
return HttpResponse(request.session[\'p1\'])
如何为我的民意调查第2页重复此顺序?

解决方法

您可能想看看
FormWizard
,
#urls.py
(r\'^(?P<pollid>\\d*)/$\',\'app.views.index\'),#views.py
def index(request,pollid):    
    latest_poll_list = Poll.objects.filter(page=pollid)
    t = loader.get_template(\'index.html\')
    c = Context({
        \'latest_poll_list\': latest_poll_list,})
    return HttpResponse(t.render(c))
也最好阅读快捷方式“ get或404” http://docs.djangoproject.com/en/dev/intro/tutorial03/?from=olddocs#a-shortcut-get-object-or-404

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...