int()的无效文字为10:'on'Python-Django [英] invalid literal for int() with base 10: 'on' Python-Django
问题描述
这是我的views.py / vote函数:
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,)))
这是错误消息屏幕:
** / errors / 2 / vote /
$ b中的ValueError
$ bint()与ba的无效文字se 10:'on'**
请求方法:POST请求URL:127.0.0.1:8000/polls/2/vote /
Django版本:1.4异常类型:ValueError异常值:
基数为10的int()的无效字面值:'on'异常
位置:/ usr / local / lib / python2 .7 / dist-packages / django / db / models / fields / init .py
在get_prep_value中,第537行
,这里是我的投票/ urls.py:
从django.conf.urls导入模式, 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'),
)
,这里是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 / $'结果'),
url(r'^(?P< poll_id> \d +)/ vote / $','vote'),
)
当您尝试时会收到此错误将一个字符串转换为一个整数,但字符串并不真正包含任何数字:
ie
number = int(string)
从你的代码中,三个地方我看到一个整数的使用和可能的演员。当 p = get_object_or_404(Poll,pk = poll_id)
时,我们假设您已正确传入一个整数作为poll_id。你可以发布你使用的urlpattern与这个视图和一个示例URL?
你也假设 request.POST [ 'choice']
将是一个整数,可以这样转换。您没有捕获与此相关的异常,因此您需要检查此条目的值。我会在这个部分添加一些其他检查:
if request.method ==POST:
choice = request.POST.get('choice',None)
如果选择不是无:
selected_choice = p.choice_set.get(pk = choice)
...
这两个脱颖而出。
请发贴你的urlpattern和你收到的错误消息(比如哪个特定的行是抛出你的异常)。
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'),
)
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).
这篇关于int()的无效文字为10:'on'Python-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!