I am following the django 1.8 tutorial and I currently finished Part 4. My
problem is when I tried to click on one of the radio buttons to vote, it
gave me a ValueError below:
ValueError at /polls/1/vote/
invalid literal for int() with base 10: '{{ choice.id }}'
Request Method:POSTRequest URL:http://127.0.0.1:8000/polls/1/vote/Django
Version:1.8.1Exception Type:ValueErrorException Value:
invalid literal for int() with base 10: '{{ choice.id }}'
Exception
Location:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.py
in get_prep_value, line 985Python Executable:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/PythonPython
Version:2.7.9Python Path:
['/Users/cellbio1/Documents/django projects/mysite',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages']
Server time:Thu, 14 May 2015 01:32:18 +0000
Traceback Switch to copy-and-paste view
<http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/core/handlers/base.py in get_response
1.
response = wrapped_callback(request,
*callback_args, **callback_kwargs)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Users/cellbio1/Documents/django projects/mysite/polls/views.py in vote
1.
selected_choice =
p.choice_set.get(pk=request.POST['choice'])
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/manager.py in manager_method
1.
return getattr(self.get_queryset(),
name)(*args, **kwargs)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/query.py in get
1.
clone = self.filter(*args, **kwargs)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/query.py in filter
1.
return self._filter_or_exclude(False, *args, **kwargs)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/query.py in _filter_or_exclude
1.
clone.query.add_q(Q(*args, **kwargs))
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/sql/query.py in add_q
1.
clause, require_inner = self._add_q(where_part,
self.used_aliases)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/sql/query.py in _add_q
1.
current_negated=current_negated,
connector=connector, allow_joins=allow_joins)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/sql/query.py in build_filter
1.
condition = self.build_lookup(lookups, col, value)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/sql/query.py in build_lookup
1.
return final_lookup(lhs, rhs)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/lookups.py in __init__
1.
self.rhs = self.get_prep_lookup()
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/lookups.py in get_prep_lookup
1.
return
self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/fields/__init__.py in get_prep_lookup
1.
return self.get_prep_value(value)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
- /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-
packages/django/db/models/fields/__init__.py in get_prep_value
1.
return int(value)
...
▶ Local vars <http://127.0.0.1:8000/polls/1/vote/#>
Here's my polls/views.py:
def vote(request, question_id):
p = get_object_or_404(Question, pk=question_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
#redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question':p,
'error_message':"You didn't select a choice",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))
Here's my polls/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$',views.ResultsView.as_view(),
name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
Here's my template/polls/detail.html:
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}"
value="{{
choice.id }}"/>
<label for="choice{{ forloop.counter }}">{{ choice.choice_text
}}</label><br/>
{% endfor %}
<input type="submit" value="Vote"/>
</form>
Here's my template/polls/results.html:
<h2>{{ question.question_text }}</h2>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{
choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
All the other pages work fine when run on the server except that my vote is
not redirected to the updated results page. I appreciate if anyone could
shed some light on this. Thanks a lot.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/5927048e-a712-4e67-b6c0-09e25e3bc6a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.