问题描述
|
我正在进行多页民意调查。我有一个数据库,该数据库有一个称为页面的列,每个页面有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