投票URL

polls/urls.py:

# ex: /polls/5/vote/
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),

创建投票form表单

polls/templates/polls/detail.html:

<h1>{{ question.question_text }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"/>
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br/>
{% endfor %}
<input type="submit" value="Vote"/>
</form>

代码解释:

{{ forloop.counter }} : for 循环次数

{% csrf_token %} : 解决POST请求跨域问题 Cross Site Request Forgeries

{% if  %}  {% endif %}:判断

{% for  %} {% endfor %}:循环

投票Views 逻辑处理

投票 views vote in polls/views.py:

from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

代码解释:

request.POST,request.GET: 分别可获得POST或GET方法的参数值,如上 request.POST['choice'] 可取得POST请求中,name值为choice的Value值。若POST参数中没有choice,则会产生KeyError。

HttpResponseRedirect:重定向跳转,提交成功后进行自动重定向跳转是web 开发的一个良好实践。防止数据提交两次。

投票结果页 Views 逻辑处理

views results in polls/views.py:

from django.shortcuts import get_object_or_404, render

def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})

投票结果显示 template

polls/templates/polls/results.html:

<h1>{{ question.question_text }}</h1>

<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul> <a href="{% url 'polls:detail' question.id %}">Vote again?</a>

实现结果

1、访问detail.html:http://localhost:8000/polls/1/

2、选择后,点击vote投票,跳转到结果页 results.html


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

最新文章

  1. CSS的常见属性
  2. CodeForces 689B Mike and Shortcuts (BFS or 最短路)
  3. Unreal Engine4 学习笔记2 动画蒙太奇
  4. paip.判断字符是否中文与以及判读是否是汉字uapi python java php
  5. 参数传递的四种形式----- URL,超链接,js,form表单
  6. activiti自定义流程之Spring整合activiti-modeler5.16实例(五):流程定义列表
  7. 【ASP.Net MVC】在AspNet Mvc使用Ajax
  8. 使用ajaxfileupload插件提示Uncaught TypeError:has no method &#39;handleError&#39;
  9. poj2352Stars
  10. MyEclipse修改servlet模版
  11. CSS清除浮动_清除float浮动
  12. 记微信开发(自定义回复&amp;关注回复)
  13. C#:求1到100的和
  14. supervisor 安装配置
  15. Maven第三篇【Maven术语、pom.xml介绍】
  16. zookeeper详解
  17. MongoDB面试题
  18. PMP知识点(四)——项目管理计划的内容
  19. 接口测试工具-poster
  20. word模板导出的几种方式:第三种:标签替换(DocX组件读取与写入Word)

热门文章

  1. 简易版自定义BaseServlet
  2. (七)Transformation和action详解-Java&amp;Python版Spark
  3. C/S打包 客户端/windows程序 Inno Setup
  4. Spring AspectJ基于注解的AOP实现
  5. win7下安装配置nodejs、使用npm安装express
  6. [LeetCode] Burst Balloons 打气球游戏
  7. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
  8. Ajax入门(二)
  9. CentOS安装Maven
  10. .NET、C#和ASP.NET,ASP.NET MVC 四者之间的区别