Invalid literal for int () with base 10: 'on' Python-Django

I am learning django from the official django tutorial. and I get this error when I vote something from the form. this is probably caused by the voting function under view.py

here is my views.py/vote function:

def vote(request,poll_id):
    p=get_object_or_404(Poll, pk=poll_id)
    try:
            selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
            return render_to_response('polls/detail.html', {'poll':p,
                                                            'error_message' : "didint select anything ",}, context_instance= RequestContext(request))

    else:
            selected_choice.votes += 1
            selected_choice.save()
            return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))

and this is the error message box:

** ValueError at / polls / 2 / vote /

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

Request Method: POST Request URL: 127.0.0.1:8000/polls/2/vote/

Django Version: 1.4 Type of exception: Exception value ValueError: invalid literal for int () with base 10: 'on' Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/ init .py in get_prep_value, line 537

and here are my polls /urls.py:

from django.conf.urls import patterns, include, url

urlpatterns = patterns ('polls.views',

    url(r'^$', 'index'),
    url(r'^(?P<poll_id>\d+)/$','detail'),
    url(r'^(?P<poll_id>\d+)/results/$','results'),
    url(r'^(?P<poll_id>\d+)/vote/$','vote'),

)

/urls.py:

from django.conf.urls import patterns, include, url

urlpatterns = patterns ('polls.views',

    url(r'^$', 'index'),
    url(r'^(?P<poll_id>\d+)/$','detail'),
    url(r'^(?P<poll_id>\d+)/results/$','results'),
    url(r'^(?P<poll_id>\d+)/vote/$','vote'),

)

+5
4

, , :

.

number = int(string)

, . p=get_object_or_404(Poll, pk=poll_id) , poll_id. URL-, , URL?

, request.POST['choice'] . - , , , . :

if request.method=="POST":
    choice = request.POST.get('choice', None)
    if choice is not None:
        selected_choice = p.choice_set.get(pk=choice)
...

.

, urlpattern , (, ).

+4

.

, . ( "polls/detail.html" ) .

+1

Yes, I had the same error, the problem explained by @cianof was in polls / detail.html templates and in other templates. The error occurs when you paste the code from the tutorial, and you have 80 field characters in your editor.

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

This will not work, because choice.id is close to braces {{choice.id }}, you should always leave a space between:{{ choice.id }}

I can’t say anything about the casting response posted by @garromark, I am new to Python and jango cooding.

+1
source

I got a similar error. The problem was hidden here:

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

All Articles