基数为 10 的 int() 的无效文字:'on' Python-Django [英] invalid literal for int() with base 10: 'on' Python-Django

查看:15
本文介绍了基数为 10 的 int() 的无效文字:'on' Python-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从官方 django 教程中学习 django.从表单投票时,我收到此错误.这可能是由 views.py 下的投票功能引起的

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

这是我的 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,)))

这是错误消息屏幕:

**ValueError at/polls/2/vote/

**ValueError at /polls/2/vote/

以 10 为基数的 int() 的无效文字:'on'**

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

请求方式:POST请求地址:127.0.0.1:8000/polls/2/vote/

Request Method: POST Request 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 行

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

这是我的 polls/urls.py:

and here is my polls/urls.py :

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

urlpatterns = patterns('polls.views',

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 :

and here is project/urls.py :

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

urlpatterns = patterns('polls.views',

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:

number = int(string)

从您的代码中,我在三个地方看到了整数的使用和可能的转换.当 p=get_object_or_404(Poll, pk=poll_id) 时,我们假设您已正确传入一个整数作为 poll_id.您能否发布与此视图相关联的 urlpattern 和示例 URL?

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?

您还假设 request.POST['choice'] 将是一个整数并且可以这样转换.您没有捕获与此相关的异常,因此您需要检查此条目的值是什么.我会为这部分添加一些其他检查:

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

这两个最突出.

请发布您的 urlpattern 以及更多您收到的错误消息(例如哪一行引发了您的异常).

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

这篇关于基数为 10 的 int() 的无效文字:'on' Python-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆