django开发的投票系统

本文对比了在Django项目中使用普通视图与通用视图的方法,详细介绍了urls配置及视图函数的具体实现,包括IndexView、DetailView和ResultsView等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不用通用视图的时候:
urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$',views.index,name='index'),
    url(r'^(?P<question_id>[0-9]+)/$',views.detail,name='detail'),
    url(r'^(?P<question_id>[0-9]+)/results/$',views.results,name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$',views.vote,name='vote'),
]

views.py:

from django.shortcuts import render,get_object_or_404
from django.http import HttpResponse,HttpResponseRedirect
from django.core.urlresolvers import reverse
from .models import Question,Choice
def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list':latest_question_list,}
    return render(request,'polls/index.html',context)
def detail(request,question_id):
    question = get_object_or_404(Question,pk=question_id)
    return render(request,'polls/detail.html',{'question':question})
def results(request,question_id):
    question = get_object_or_404(Question,pk=question_id)
    return render(request,'polls/results.html',{'question':question})
def vote(request,question_id):
    p = get_object_or_404(Question,pk=question_id)
    try:
        selected_choice = p.Choice_set.get(pk=request.POST['choice'])
    except (KeyError,Choice.DoesNotExist):
        return render(request,'polls/detail.html',{'question':p,'error_message':'You did not select a choice .'})
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('results',args=(p.id,))

使用通用视图时:
urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$',views.IndexView.as_view(),name='index'),
    url(r'^(?P<pk>[0-9]+)/$',views.DetailView.as_view(),name='detail'),
    url(r'^(?P<pk>[0-9]+)/results/$',views.ResultsView.as_view(),name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$',views.vote,name='vote'),
]

views.py

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

from django.core.urlresolvers import reverse
from django.views import generic

from .models import Question,Choice

# Create your views here.
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        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'

def vote(request,question_id):
    p = get_object_or_404(Question,pk=question_id)
    try:
        selected_choice = p.Choice_set.get(pk=request.POST['choice'])
    except (KeyError,Choice.DoesNotExist):
        return render(request,'polls/detail.html',{'question':p,'error_message':'You did not select a choice .'})
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('results',args=(p.id,)))

detail.html:

<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action='{% url '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>

index.html:

{% if latest_question_list %}
<ul>
    {%for question in latest_question_list %}
    <li><a href="{% url 'detail' question.id %}">{{question.question_text}}</a></li>
    {% endfor %}
</ul>
{% else %}
<p>No polls are avalible.</p>
{% endif %}

results.py:

<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 %}

<a href="{% url 'detail' question.id %}">Vote again?</a>
</ul>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值