Friday, July 8, 2016

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

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.


No comments:

Post a Comment