Django 1.8 tutorial-Chapter4: ValueError at /polls/1/vote/

282 views
Skip to first unread message

charito.romeo

unread,
May 14, 2015, 7:16:07 AM5/14/15
to django...@googlegroups.com
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:POST
Request URL:http://127.0.0.1:8000/polls/1/vote/
Django Version:1.8.1
Exception Type:ValueError
Exception 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 985
Python Executable:/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version:2.7.9
Python 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

  • /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)
      ...
  • /Users/cellbio1/Documents/django projects/mysite/polls/views.py in vote
    1.                     selected_choice = p.choice_set.get(pk=request.POST['choice'])
      ...
  • /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)
      ...
  • /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)
      ...
  • /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)
      ...
  • /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))
      ...
  • /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)
      ...
  • /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)
      ...
  • /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)
      ...
  • /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)
      ...
  • /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()
      ...
  • /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)
      ...
  • /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)
      ...
  • /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)
      ...

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.




Alasdair Nicol

unread,
May 14, 2015, 7:57:48 AM5/14/15
to django...@googlegroups.com
On 14/05/15 02:50, charito.romeo wrote:
> |
> <input type="radio" name="choice" id="choice{{ forloop.counter }}"
> value="{{
> choice.id }}"/>
> |

The problem is that there is a new line in the middle of

"{{ choice.id }}".

Change it to:

<input type="radio" name="choice" id="choice{{ forloop.counter }}"
value="{{ choice.id }}"/>

Cheers,
Alasdair

--
Alasdair Nicol
Developer, MEMSET

mail: alas...@memset.com
web: http://www.memset.com/

Memset Ltd., registration number 4504980.
Building 87, Dunsfold Park, Stovolds Hill, Cranleigh, Surrey, GU6 8TB, UK

Charito Romeo

unread,
May 14, 2015, 9:06:43 AM5/14/15
to django...@googlegroups.com
Hi Alasdair,

Thanks a lot for the help. It's working now. Cheers :)

Charito



--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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/55548D93.3000701%40memset.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages