On Jun 14, 2:13 pm, Sithembewena Lloyd Dube <zebr...@gmail.com> wrote:
> 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['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('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're trying to run before you can walk. Finish the tutorial, then
read the forms framework documentation (http://docs.djangoproject.com/
en/1.2/topics/forms/) which will explain how Django does all this for
you.
--
DR.

-- 
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 at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to