django学习入门四:继续完善view视图,并介绍django的通用视图

      上一节,我们简单介绍了一下index视图,其他三个视图没有补全,现进行说明:

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>
说明:error_message是当view中捕获到异常后,填写error_message信息,然后回传回页面

form表单中有  单选按钮,label,按钮,采用post方式发送,并且url发送添加token,防止url被篡改

from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse

from .models import Choice, Question
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):
        return render(request, 'polls/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
说明:异常捕获两种,KeyError和DoesnotExist,前者是说如果前端页面没有传递name='choice'的参数,就会捕获KeyError的值,如果question中没有查到符合要求的choice,会触发 DoesnotExist异常。并会将question和error_message回传到polls/details.html

        如果没有异常,则会将符合要求的choice值+1,然后保存数据库,并返回给前端页面polls:results显示

修改polls/view.py中results方法

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question': question})
说明:results页面显示投票结果,按照话题查找所有的投票选项并显示。

添加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>
说明:同上

这样如果没有语法错误就可以正常显示了

数据库没有选项,会提示错误信息

有相应的投票选项,正常显示,并可以投票


至此,官网提供的投票的小程序就完成其功能了,但是官网没完,还提供了更方便的django通用视图来重构现有的视图

代码如下:

polls.urls

    # ex: /polls/
    path('', views.IndexView.as_view(), name='index'),
    # ex: /polls/5/
    path('<int:pk>/', views.DetailView.as_view(), name='detail'),
    # ex: /polls/5/results/
    path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
说明:<int:question_id>改成<int:pk>

         <views.detail>改成<view.DetailView.as_view()>

from django.views import generic
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions."""
        return Question.objects.order_by('-pub_date')[:5]


class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'


class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

说明:使用了django的通用视图generic.DetailView和ListView

           之前的方法由相应的类取代,并且继承了generic下的视图类

运行后,效果一致,但实现起来更加简洁

ps:

我们将Question 和Choice 模型都注册到polls/admin中,就会在admin界面显示两个模型,这样添加起来非常方便












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值