Friday, August 16, 2013

Re: Can't seem to get "votes" to work right

On Friday, 16 August 2013 16:40:24 UTC+1, bud wrote:
Hi,

I've finished the Django tutorial but I just can't seem to get my "votes" function to work right. I would start the server, go to polls, and every time I try to vote for something, the KeyError message comes up i.e. "You didn't select a choice." when I did. Maybe if there was some way to define these choices it might fix it, but I'm not sure.
 
<snip>
and detail.html

<h1>{{ poll.question }}</h1>

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

Any ideas?


The problem is in your template, where you define the choice field. You've added {{ forloop.counter }} to the name of the field, which means that there's no key "choice" in the POST data: there's only "choice1", "choice2" etc. Each button in the radio input needs to have the same name, although they obviously have different values.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate