首页 文章

int()的基数为10的无效文字:'on' Python-Django

提问于
浏览
6

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

这是我的views.py /投票功能:

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

这是错误消息屏幕:

**在/ polls / 2 / / /无效文字中的ValueError对于int(),基数为10:'on'**请求方法:POST请求URL:127.0.0.1:8000/polls/2/vote/Django版本:1.4异常类型:ValueError异常值:int()的基数为10的无效文字:'on'异常位置:/usr/local/lib/python2.7/dist-packages/django/db/models/fields/init.py in get_prep_value,第537行

这是我的民意调查/ 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'),

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

5 回答

  • 0

    当您尝试将字符串转换为整数时,您将收到此错误,但该字符串实际上不包含任何数字:

    number = int(string)
    

    从您的代码中,有三个地方我看到整数的使用和可能的强制转换 . 当 p=get_object_or_404(Poll, pk=poll_id) 我们're making an assumption that you'已正确传递一个整数作为poll_id . 您可以发布与此视图关联的urlpattern以及示例网址吗?

    您还假设 request.POST['choice'] 将是一个整数,并且可以这样投射 . 你不想't catching an exception related to this, so you'我想检查这个条目的 Value 是多少 . 我会为这部分添加一些其他检查:

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

    这两个最突出 .

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

  • 2

    我也有这个错误 .

    我的情况是我的表格模板中有拼写错误 . 请仔细检查投票明细模板(“polls / detail.html”)是否存在拼写错误 .

  • 2

    是的,我有同样的错误,@ cianof解释的问题出现在模板polls / detail.html和其他模板中 . 粘贴教程中的代码并且编辑器中有80个字符的边距时会发生错误 .

    <input type="radio" name="choice" id="choice {{ forloop.counter }}" value="{{
    choice.id }}" />
    

    这不起作用,因为choice.id接近大括号 {{choice.id }} ,你应该总是留一个空格: {{ choice.id }}

    关于@garromark发布的演员答案,我不能说什么,我是Python和Django编码的新手 .

  • 5

    我收到了类似的错误 . 问题隐藏在这里:

    def results(request, question_id):
        question = get_object_or_404(Question, pk=question_id)
        return render(request, 'polls/results.html', {'question: question'})
    
  • 0

    我发生了同样的错误 . 我发现了什么是错的,结果只是一些小细节:首先,在我的'polls / detail.html'模板中,有一个输入:

    <input type="radio" name="choice" id="choice {{forloop.counter}}" value="{{choice.id}}"/> 在's why it didn' t之前我得到了 {{choice_id}} .

    第二,另一个错误说我的'polls / result.html'没有找到,然后我进入这个模板我发现了一个愚蠢的错误:

    <a href="{% url 'polls:detail' question.id %}">Vote again?</a>

    在标签内部,我有'polls.detail',难怪为什么找不到模板 . ;)

相关问题