ValueError: invalid literal for int() with base 10: 'on'

564 views
Skip to first unread message

Gary Roach

unread,
Jul 8, 2016, 7:43:26 PM7/8/16
to django-users

Hi all,

I'm working on the official django 1.9 tutorial part 4 , Python 3.5 and Django 1.9 are encapsulated in a virtualenv file in my /opt/ directory.

OS Debian Linux with KDE desktop

IDE Eclipse with PyDev plugin.

Every time I try to vote the program errors out with the following:

  File "/opt/python-virtual-environments/py3.5dj1.9.7/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'on'
[07/Jul/2016 16:21:41] "POST /polls/1/vote/ HTTP/1.1" 500 120688

The code at line 976 is:

    def get_prep_value(self, value):
        value = super(AutoField, self).get_prep_value(value)
        if value is None:
            return None
        return int(value)

My project layout is:

mysite

    src

        mysite

            ...

            settings.py

            urls.py

            wsgi.py

        polls

            migrations

            templates

                polls

                    detail.html

                    index.html

                    results.html

            admin.py

            apps.py

            models.py

            urls.py

            views.py

            db.sqlite3

        manage.py



My mysite.settings file Installed apps looks like:

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

My polls.views file is :

#Include any python library functions you wish.

from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.core.urlresolvers import reverse


from polls.models import Choice, Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list }
    return render(request, 'polls/index.html', context)

def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})


def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question':question})


def vote(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(
                          pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/detail.html',
                      {'question': question, 'error_message':
                       "You didn't select a choice.",}
                      )
    else:
        selected_choice.votes += 1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results',
               args=(question.id,)))

The polls,detail.html is:

<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>

I am not very familiar with the Eclipse PyDev debugger and can't seem to find the problem. Any help will be sincerely appreciated.

Gary R.


James Schneider

unread,
Jul 8, 2016, 7:59:24 PM7/8/16
to django...@googlegroups.com

> The polls,detail.html is:
>
> <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 }}" />

It looks like your value attribute is missing an equals (=) sign. I think that the value is set to 'on' if the box is selected by the user and you haven't specified a value attribute.

If that's not it, can you post the entire traceback? This is an error from within the Django framework itself, likely from receiving a str value when it was expecting an int. The full traceback will hopefully tell us what portion of your code made the initial call.

-James

Gary Roach

unread,
Jul 8, 2016, 10:43:16 PM7/8/16
to django...@googlegroups.com
Thank you.

I mistyped the code string. and left out the = sign.. Things work as they should now. I guess I need to be a little more careful when proofing my code.

Thanks again

Gary Rl

--

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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWu76QWG4WTzr57T3euwRvi1eDwfFiUAT9q1wiWDSiP1Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages