zoukankan      html  css  js  c++  java
  • Django基础,Day5

    投票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测试开发圈”,了解更多测试教程!***
  • 相关阅读:
    HBase Cassandra比较
    重新认识HBase,Cassandra列存储——本质是还是行存储,只是可以动态改变列(每行对应的数据字段)数量而已,当心不是parquet
    HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!
    Cassandra 数据模型设计,根据你的查询来制定设计——反范式设计本质:空间换时间
    【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II
    LVM逻辑卷管理命令
    Java引进和应用的包装类
    Android 4.0新组件:GridLayout详细说明
    【剑指offer】打印单列表从尾部到头部
    原因以及如何避免产生僵尸进程
  • 原文地址:https://www.cnblogs.com/guanfuchang/p/6256879.html
Copyright © 2011-2022 走看看