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

2024/10/12 18:23:16

i am learning django from official django tutorial. and i am getting this error when vote something from form. this caused from - probably - vote function under views.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 += 1selected_choice.save()return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))

and this is error message screen :

**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 Exception Type: ValueError Exception Value: invalid literal for int() with base 10: 'on' ExceptionLocation: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.pyin get_prep_value, line 537

and here is 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'),

)

and here is project/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'),

)

Answer

You'll receive this error when you're trying to cast a string to an integer, but the string doesn't really contain any digits:

i.e.

number = int(string)

From your code, there are three places where I see the use and probable cast of an integer. When p=get_object_or_404(Poll, pk=poll_id) we're making an assumption that you've correctly passed in an integer as poll_id. Could you post the urlpattern you're using associated with this view and an example URL?

You also are making an assumption that request.POST['choice'] will be an integer and can be cast as such. You aren't catching an exception related to this, so you'll want to check what the value of this entry is. I would add in a few other checks for this part:

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

These two stand out the most.

Please post your urlpattern and more of the error message you were getting (such as which specific line is throwing your exception).

https://en.xdnf.cn/q/69626.html

Related Q&A

How to use HTTP method DELETE on Google App Engine?

I can use this verb in the Python Windows SDK. But not in production. Why? What am I doing wrong?The error message includes (only seen via firebug or fiddler)Malformed requestor something like thatMy…

Python heapq : How do I sort the heap using nth element of the list of lists?

So I have lists of lists getting added to the heap; eg:n = [[1, 5, 93],[2, 6, 44],[4, 7, 45],[6, 3, 12]]heapq.heapify(n)print(n)This compares and sorts according to the lists first element.My question …

How strings are stored in python memory model

I am from c background and a beginner in python. I want to know how strings are actually stored in memory in case of python.I did something likes="foo"id(s)=140542718184424id(s[0])= 140542719…

The _imaging C module is not installed (on windows)

Im trying to generate some pdf with django/PIL/Imaging and everything is good until I attempt to put some images into the pdf:Exception Type: ImportError Exception Value: The _imaging C module is n…

HOW TO use fabric use with dtach,screen,is there some example

i have googled a lot,and in fabric faq also said use screen dtach with it ,but didnt find how to implement it? bellow is my wrong code,the sh will not execute as excepted it is a nohup taskdef dispatc…

Developing for the HDMI port on Linux

How would it be possible to exclusively drive the HDMI output from an application, without allowing the OS to automatically configure it for display output?For example, using the standard DVI/VGA as t…

Hive client for Python 3.x

is it possible to connect to hadoop and run hive queries using Python 3.x? I am using Python 3.4.1.I found out that it can be done as written here: https://cwiki.apache.org/confluence/display/Hive/Hiv…

How to add a function call to a list?

I have a Python code that uses the following functions:def func1(arguments a, b, c):def func2(arguments d, e, f):def func3(arguments g, h, i):Each of the above functions configures a CLI command on a p…

How to do fuzzy string search without a heavy database?

I have a mapping of catalog numbers to product names:35 cozy comforter 35 warm blanket 67 pillowand need a search that would find misspelled, mixed names like "warm cmfrter".We have code u…

Logging while nbconvert execute

I have a Jupyter notebook that needs to run from the command line. For this I have the following command:jupyter nbconvert --execute my_jupyter_notebook.ipynb --to pythonThis command creates a python s…