django开发的投票系统
2016-04-11 21:58
531 查看
不用通用视图的时候:
urls.py:
views.py:
使用通用视图时:
urls.py:
views.py
detail.html:
index.html:
results.py:
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中使用jquery ajax post数据出现403错误的解决办法(两种方法)
- django使用ajax post数据出现403错误如何解决
- windows+apache+mod_python配置django运行环境
- python Django连接MySQL数据库做增删改查
- django自定义Field实现一个字段存储以逗号分隔的字符串
- Django框架利用ajax实现批量导入数据功能
- python Django批量导入数据
- 在Python的Django框架中加载模版的方法
- 在Django中使用Sitemap的方法讲解
- 详解Django中的form库的使用
- Django中的CACHE_BACKEND参数和站点级Cache设置
- 剖析Django中模版标签的解析与参数传递
- Python中DJANGO简单测试实例
- Python的Django框架中的数据库配置指南
- 在Django的上下文中设置变量的方法
- 在Python的Django框架中编写编译函数
- 以一个投票程序的实例来讲解Python的Django框架使用
- 在Django的URLconf中进行函数导入的方法
- 在Python的Django框架中生成CSV文件的方法
- 在主机商的共享服务器上部署Django站点的方法