1 | from django.http import HttpResponse,Http404,HttpResponseRedirect
|
---|
2 | from django.template import RequestContext, loader
|
---|
3 | from django.shortcuts import render,get_object_or_404,render_to_response
|
---|
4 | from polls.models import *
|
---|
5 | from django.core.urlresolvers import reverse
|
---|
6 |
|
---|
7 | def index_4(request):
|
---|
8 | latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
|
---|
9 | context = {'latest_poll_list': latest_poll_list}
|
---|
10 | return render(request, 'polls/index.html', context)
|
---|
11 | def index_3(request):
|
---|
12 | latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
|
---|
13 | template = loader.get_template('polls/index.html')
|
---|
14 | context = RequestContext(request, {
|
---|
15 | 'latest_poll_list': latest_poll_list,
|
---|
16 | })
|
---|
17 | return HttpResponse(template.render(context))
|
---|
18 |
|
---|
19 | def index_2(request):
|
---|
20 | latest_poll_list = Poll.objects.order_by('-pub_date')[:5]
|
---|
21 | output = ', '.join([p.question for p in latest_poll_list])
|
---|
22 | return HttpResponse(output)
|
---|
23 | ##---------------------------------------------------------------------------------------------------------------------------------------------------------------------##
|
---|
24 | def detail_4(request, poll_id):
|
---|
25 | p = get_object_or_404(Poll, pk=poll_id)
|
---|
26 | return render_to_response('polls/detail.html', {'poll': p},
|
---|
27 | context_instance=RequestContext(request))
|
---|
28 |
|
---|
29 | def detail_3(request, poll_id):
|
---|
30 | poll = get_object_or_404(Poll, pk=poll_id)
|
---|
31 | return render(request, 'polls/detail.html', {'poll': poll})
|
---|
32 |
|
---|
33 | def detail_2(request, poll_id):
|
---|
34 | try:
|
---|
35 | poll = Poll.objects.get(pk=poll_id)
|
---|
36 | except Poll.DoesNotExist:
|
---|
37 | raise Http404
|
---|
38 | return render(request, 'polls/detail.html', {'poll': poll})
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | def detail_1(request, poll_id):
|
---|
43 | return HttpResponse("You're looking at poll %s." % poll_id)
|
---|
44 | ##---------------------------------------------------------------------------------------------------------------------------------------------------------------------##
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | def results_2(request, poll_id):
|
---|
49 | p = get_object_or_404(Poll, pk=poll_id)
|
---|
50 | return render_to_response('polls/results.html', {'poll': p})
|
---|
51 |
|
---|
52 |
|
---|
53 | def results_1(request, poll_id):
|
---|
54 | return HttpResponse("You're looking at the results of poll %s." % poll_id)
|
---|
55 | ##---------------------------------------------------------------------------------------------------------------------------------------------------------------------##
|
---|
56 |
|
---|
57 |
|
---|
58 | def vote(request, poll_id):
|
---|
59 | p = get_object_or_404(Poll, pk=poll_id)
|
---|
60 | try:
|
---|
61 | selected_choice = p.choice_set.get(pk=request.POST['choice'])
|
---|
62 | except (KeyError, Choice.DoesNotExist):
|
---|
63 | # Redisplay the poll voting form.
|
---|
64 | return render_to_response('polls/detail.html', {
|
---|
65 | 'poll': p,
|
---|
66 | 'error_message': "You didn't select a choice.",
|
---|
67 | }, context_instance=RequestContext(request))
|
---|
68 | else:
|
---|
69 | selected_choice.votes += 1
|
---|
70 | selected_choice.save()
|
---|
71 | # Always return an HttpResponseRedirect after successfully dealing
|
---|
72 | # with POST data. This prevents data from being posted twice if a
|
---|
73 | # user hits the Back button.
|
---|
74 | #return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))
|
---|
75 | return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))#***Here change is to be replace view as varibale ie generic***#
|
---|
76 | def vote_2(request, poll_id):
|
---|
77 | p = get_object_or_404(Poll, pk=poll_id)
|
---|
78 | try:
|
---|
79 | selected_choice = p.choice_set.get(pk=request.POST['choice'])
|
---|
80 | except (KeyError, Choice.DoesNotExist):
|
---|
81 | # Redisplay the poll voting form.
|
---|
82 | return render_to_response('polls/detail.html', {
|
---|
83 | 'poll': p,
|
---|
84 | 'error_message': "You didn't select a choice.",
|
---|
85 | }, context_instance=RequestContext(request))
|
---|
86 | else:
|
---|
87 | selected_choice.votes += 1
|
---|
88 | selected_choice.save()
|
---|
89 | # Always return an HttpResponseRedirect after successfully dealing
|
---|
90 | # with POST data. This prevents data from being posted twice if a
|
---|
91 | # user hits the Back button.
|
---|
92 | return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))
|
---|
93 |
|
---|
94 |
|
---|
95 | def vote_1(request, poll_id):
|
---|
96 | return HttpResponse("You're voting on poll %s." % poll_id)
|
---|