您的位置:首页 > 编程语言 > Go语言

django开发的投票系统

2016-04-11 21:58 531 查看
不用通用视图的时候:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django