Django基础,Day5 - form表单投票详解

投票URL

polls/urls.py:

1
2
# ex: /polls/5/vote/
  url(r '^(?P<question_id>[0-9]+)/vote/$' , views.vote, name = 'vote' ),
创建投票form表单

polls/templates/polls/detail.html:

1
2
3
4
5
6
7
8
9
10
11
12
< 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

1
2
3
4
5
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:

1
2
3
4
5
6
7
8
9
< 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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值