Skip to content

Instantly share code, notes, and snippets.

@robc
Created March 23, 2009 10:59
Show Gist options
  • Save robc/83510 to your computer and use it in GitHub Desktop.
Save robc/83510 to your computer and use it in GitHub Desktop.
from helloworld.polls.models import Poll
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list})
def detail(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
return render_to_response('polls/detail.html', {'poll':poll})
def vote(request, poll_id):
poll = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the poll voting form.
return render_to_response('polls/detail.html', { 'poll': p, 'error_message': "You didn't select a choice." })
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('helloworld.polls.views.results', args=(p.id,)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment