﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21769	Error message displayed falsely in poll tutorial	timdaman	nobody	"I am working through the tutorial ""poll"" project and found a very minor bug in the example code. In section 4,

https://docs.djangoproject.com/en/1.6/intro/tutorial04/

the vote method erroneously displays the error message when the vote form is initially loaded. This does not make sense since the user has not yet attempted to submit form data. In a real world setting this could cause user confusion and frustration as ""Looking at a blank form"" is != to ""Submitting a blank form"" and the messages each requires is different (""Please make a choice"" vs. ""Oops, you forgot to make a choice""). 

Below is the modification I made to detect this difference and display the correct output. I am new to django and my last web editing job was in the 90's so I welcome suggestions for improvements of my code!


{{{
def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)

    if request.method == ""POST"":
        try:
            selected_choice = p.choice_set.get(pk=request.POST['choice'])
        except (KeyError, Choice.DoesNotExist):
            # Redisplay the poll voting form.

            return render(request, 'polls/detail.html', {
                'poll': p,
                'error_message': ""You didn't select a choice."",
            })

        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:results', args=(p.id,)))
    else:
        return render(request, 'polls/detail.html', {
            'poll': p,
        })
}}}

"	Bug	closed	Documentation	1.6	Normal	invalid	tutorial		Unreviewed	0	0	0	0	1	0
