Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Django tutorial: no error displayed when a choice for a poll is not selected. [RSS Feed]

#1 June 14, 2010 14:13:35

Sithembewena L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Django tutorial: no error displayed when a choice for a poll is not selected.


Hi all,

I have a "vote" view and "detail" template in the Polls application that I
am writing. The idea is that the user will see a "vote" page where they can
select one of two radio buttons for a choice to the poll, and click "vote".
This takes them to a "results" page where they can view the poll and its
choices. That works fine. However, if a choice is not selected, the user
should be sent to the "detail" page, where an error message will be shown
stating that no choice was selected. This is not happening - when I haven't
selected a choice, the "vote" page is reloaded upon submitting the form.

Please see the vote view and detail template code below:

*VOTE VIEW*
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST)
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('myproject.polls.views.results',
args=(p.id,)))



*DETAIL TEMPLATE*
<h1>{{ poll.question }}</h1>

*{% if error_message %}<p><strong>{{ error_messsage }}</strong></p>{% endif
%}
*
<form action="/polls/{{ poll.id }}/vote/" method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}"
value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice
}}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

Any anomalies here?

Thanks.

--
Regards,
Sithembewena Lloyd Dubehttp://www.lloyddube.com--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Django tutorial: no error displayed when a choice for a poll is not selected. [RSS Feed]

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support