我是django的新手,我已经以与“投票”项目教程完全相同的方式设置了我的项目。通过按钮提交选项时,我试图刷新页面[[not或将其重定向到另一个视图。
这是我的detail.html
: (...)
<div class="cardq mt15">
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<!-- New Question -->
<div class="cardq__qu">
<i class="far fa-question-circle"></i>
<!--<p name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
</p>-->
<input type="submit" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"> </div>
{% endfor %}
(...)
我在我的[[views.py:上有这个
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,)))
我基本上想要的是不要重定向到results.html,并在detail.html 无需刷新页面
上显示结果,它具有投票按钮。它已经可以在同一页面上准确显示投票结果,但是如果我单击其中一个按钮,则会强制将其重定向到结果页面。