【问题标题】:Django tutorial : TypeError at /polls/3/vote/ reverse() takes no keyword argumentsDjango 教程:/polls/3/vote/reverse() 的 TypeError 不接受关键字参数
【发布时间】:2017-06-29 15:21:06
【问题描述】:

我一直在使用教程处理 django 项目,我收到了这条消息。 当我点击单选框进行投票时,admin 中的计数上升,但站点显示错误消息而不是加载模板。看来 model.py 中有问题所以我添加了它。

TypeError at /polls/3/vote/
reverse() takes no keyword arguments

Request Method: POST
Request URL:    http://127.0.0.1:8000/polls/3/vote/
Django Version: 1.10.5
Exception Type: TypeError
Exception Value:    reverse() takes no keyword arguments
Exception Location: C:\Users\jeon hyun joo\workspace\ch3\polls\views.py in vote, line 33
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.13
Python Path:    
['C:\\Users\\jeon hyun joo\\workspace\\ch3',
 'C:\\Users\\jeon hyun joo\\workspace\\ch3',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\django-1.10.5-py2.7.egg',
 'C:\\WINDOWS\\SYSTEM32\\python27.zip',
 'C:\\Python27\\lib\\plat-win']

views.py

from audioop import reverse
from gc import get_objects

from django.http.response import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.template.context_processors import request

from polls.models import Question, Choice


# Create your views here.
def index(request):
   latest_question_list = Question.objects.all().order_by('-pub_date')[:5]
   context = {'latest_question_list': latest_question_list}
   return render(request, 'polls/index.html', context)

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

def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
    selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
    return render(request, 'polls/detail.html', {
        'question' : question,
        'error_message' : "You didn't select a choice",
        })
else:
    selected_choice.votes += 1
    selected_choice.save()
    return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))    

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

我在 Eclipse 中添加了从控制台收到的消息

TypeError: reverse() takes no keyword arguments
[11/Feb/2017 16:50:30] "POST /polls/3/vote/ HTTP/1.1" 500 67663

【问题讨论】:

    标签: python django eclipse


    【解决方案1】:

    您正在使用这行代码覆盖 django reverse 方法:

    from audioop import reverse
    

    要使用 django 的反向,你应该导入它:

    from django.urls import reverse
    

    如果您仍然需要 audioop 的反向,您可以使用 as 语法的同义词:

    from audioop import reverse as audio_reverse
    

    【讨论】:

    • 这个解决方案解决了问题,但现在显示不同的错误消息,说 AttributeError,AttributeError at /polls/3/results/ 'function' object has no attribute 'META'。
    • @camila 你这里有错字:def results(reqeust, question_id):reqeustrequest 的插入。看起来您也可以删除此行:from django.template.context_processors import request you don't use it in code.
    猜你喜欢
    • 2020-05-17
    • 2019-01-04
    • 2018-11-05
    • 2016-01-14
    • 2016-12-24
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    相关资源
    最近更新 更多