问题
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 += 1
selected_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' Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py in 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'),
)
回答1:
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).
回答2:
Yes, I had the same error, the problem as explained by @cianof was in the template polls/detail.html and in others templates. The error occurs when you paste the code from the tutorial and you have 80 character of margin in your editor.
<input type="radio" name="choice" id="choice {{ forloop.counter }}" value="{{
choice.id }}" />
This will not works because choice.id is close to the braces {{choice.id }}
, you should always leave a space between: {{ choice.id }}
About the casting answer posted by @garromark, I can't say anything, I'm new in Python and Django cooding.
回答3:
I had this error too.
My my case it I had a typo in my form template. Double check the poll detail template (“polls/detail.html”) for typos.
回答4:
I was receive similar error. 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'})
回答5:
I had the same error occurred. I found out what was wrong, turns out it was just some little details: First,In my 'polls/detail.html' template, there is an input:
<input type="radio" name="choice" id="choice {{forloop.counter}}" value="{{choice.id}}"/>
I had {{choice_id}}
before that's why it didn't get the value.
Second, another error said my 'polls/result.html' was not found, then I went into this template I found a stupid mistake:
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
Inside the tag, I had 'polls.detail', no wonder why the template was not found. ;)
来源:https://stackoverflow.com/questions/11387424/invalid-literal-for-int-with-base-10-on-python-django