Python Django TypeError at/polls/1/vote/_reverse_,在*后面加上_prefix()参数必须是一个iterable,而不是int
这是来自的轮询应用程序教程 当我转到第一个问题时,选择一个选项并单击“投票”,我会收到错误消息 views.py:Python Django TypeError at/polls/1/vote/_reverse_,在*后面加上_prefix()参数必须是一个iterable,而不是int,python,django,django-forms,django-templates,django-views,Python,Django,Django Forms,Django Templates,Django Views,这是来自的轮询应用程序教程 当我转到第一个问题时,选择一个选项并单击“投票”,我会收到错误消息 views.py: from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.views import generic from .models import Choice
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from .models import Choice, Question
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
# request.POST['choice'] returns ID of the selected choice as a string
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
# Always return a HttpResponseRedirect after successfully dealing with POST data.
# This prevents the data from being posted twice if a user hits the Back button.
return HttpResponseRedirect(reverse('polls:results', args=question_id, ))
polls/url.py:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
polls/templates/polls/index.html:
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
polls/templates/polls/detail.html:
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"/>
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
<input type="submit" value="Vote"/>
</form>
polls/templates/polls/results.html:
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
有人能帮忙吗?问题是你写了:
return HttpResponseRedirect(reverse('polls:results', args=question_id, ))
注意:在他们编写的参数中,args=question\u id,。这与args=question_id有所不同。在Python中,0不是整数,而是包含一个元素的1元组:0。简言之:括号很重要
但是没有必要做所有这些包装。Django有一个shorcut,以更方便的方式构建HttpResponseRedirects:
return redirect('polls:results', question_id)
这将*args和**kwargs本身作为位置参数和命名参数。因此,您可以编写它,就好像您正在以函数的形式直接调用视图一样,并将视图的名称放在前面。args应该包含一个item的iterable,而不是单个item。args=question\u id,应该是args=[question\u id]
return redirect('polls:results', question_id)