Looking for a round trip example of adding/editing/saving a record using class-based views

19 views
Skip to first unread message

Lee Hinde

unread,
Mar 18, 2012, 7:11:31 PM3/18/12
to django...@googlegroups.com
I'm looking for something like the vote function in step 4 of the tutorial, but using class-based views.py. Any pointers would be appreciated.

Thanks.

Lee Hinde

unread,
Mar 19, 2012, 7:21:36 PM3/19/12
to django...@googlegroups.com

Specifically, what's the equivalent of this block:

def vote(request, poll_id):
p = 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.",
}, context_instance=RequestContext(request))
else:
selected_choice.votes += 1
selected_choice.save()
# Always return an HttpResponseRedirect after successfully dealing
# with POST data. This prevents data from being posted twice if a
# user hits the Back button.
return HttpResponseRedirect(reverse('polls.views.results',
args=(p.id,)))

Reply all
Reply to author
Forward
0 new messages