python里int为什么显示无效文字,int()的无效文字为10:'on'Python-Django

本文探讨了在使用Django官方教程学习过程中遇到的一个具体错误:尝试从表单投票时出现的ValueError。该错误源自试图将字符串转换为整数但字符串实际不包含数字的情况。文章详细分析了可能的原因,并提供了代码修改建议。

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\d+)/$','detail'),

url(r'^(?P\d+)/results/$','results'),

url(r'^(?P\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\d+)/$','detail'),

url(r'^(?P\d+)/results/$','results'),

url(r'^(?P\d+)/vote/$','vote'),

)

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值