简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM