我得到以下错误:
Reverse for 'vote' with no arguments not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']在polls.views中
def vote(request, question_id):
question = get_object_or_404( Questions, 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(request, question_id):
question = get_object_or_404(Questions, pk = question_id)
return render(request,'polls/result.html',{'question':question})Polls url_patterns
urlpatterns = [
path('',views.index,name='index'),
path('<int:question_id>/',views.details, name='details'),
path('<int:question_id>/results/',views.results, name='results'),
path('<int:question_id>/vote/',views.vote, name='vote')
]和polls/result.html
<h1>{{question.question_text}}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{choice.choice_text}} -- {{choice.votes}} votes {{choice.votes|pluralize}}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:vote' %}">Vote Again?</a>发布于 2020-12-27 17:23:14
您缺少vote反向url模式中的question_id。下面的url必须是整型id
path('<int:question_id>/vote/',views.vote, name='vote')您没有在html模板中的反向URL中提供。
<a href="{% url 'polls:vote' %}">Vote Again?</a> 像这样传递vote_id
<a href="{% url 'polls:vote' vote_id %}">Vote Again?</a> https://stackoverflow.com/questions/65464260
复制相似问题