Django教程:/ poll / 3 / vote / reverse()中的TypeError没有关键字参数 [英] Django tutorial : TypeError at /polls/3/vote/ reverse() takes no keyword arguments

查看:293
本文介绍了Django教程:/ poll / 3 / vote / reverse()中的TypeError没有关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用教程进行django项目,我收到了这个消息。
当我点击无线电框投票时,管理员的计数上升,但网站显示错误消息,而不是loeading模板。似乎在module.py中有问题,所以我添加了。

  Type / at / polls / 3 / vote / 
reverse()没有关键字参数

请求方法:POST
请求URL:http://127.0.0.1:8000/polls/3/vote/
Django版本:1.10.5
异常类型:TypeError
异常值:reverse( )没有关键字参数
异常位置:C:\Users\jeon hyun joo\workspace\ch3\polls\views.py投票,第33行
Python可执行文件:C: \Python27\python.exe
Python版本:2.7.13
Python路径:
['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-包',
'C:\\Python27\\\\\\\\site-packages\\\\ django-1.10.5-py2.7.egg',
'C:\\WINDOWS\\\SYSTEM32\\python27.zip',
'C:\\ Python27\\lib\\plat-win']

views.py

从audioop import reverse 
从gc导入get_objects

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

from polls.models import Question,Choice


#在这里创建您的意见。
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':你没有选择,
})
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()没有关键字参数
[11 / Feb / 2017 16:50 :30]POST / polls / 3 / vote / HTTP / 1.1500 67663


解决方案

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

  c>从audioop import reverse 

要使用django的相反,您应该导入它:

  from django.urls进口反向

如果您仍然需要audioop的相反,您可以使用与同义词作为语法:

  from audioop import reverse as audio_reverse 


I've been working on django project with tutorials and I got this message. When I click radio box to vote, the count in admin goes up but site shows error message instead of loeading templates.It seems like there is something wrong in model.py so I added it.

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

I added message I got from console in eclipse

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

解决方案

You are overriding django reverse method with this line of code:

from audioop import reverse

to use django's reverse you should import it:

from django.urls import reverse

also if you still need audioop's reverse you can use synonym with as syntax:

from audioop import reverse as audio_reverse

这篇关于Django教程:/ poll / 3 / vote / reverse()中的TypeError没有关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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