Django没有在我的Django教程中返回'结果模板'

时间:2017-07-30 13:36:17

标签: django templates url

我正在使用web-poll应用程序教程来更多地了解Django框架。

在我在polls / views.py中创建投票方法并创建关联的polls / templates / index.html,details.html,results.html模板之后的教程中,我想提出一个问题,使用单选按钮选择3个选项,并使用投票按钮提交我的选择,该选项可在details.html模板中找到。

在我选择一个选项并单击投票按钮后,我将查看我在results.html模板中投票选择特定选项的次数。

在我的民意调查/ 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):
        # 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 an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

这是为了确保在投票时我看到指标,如果我单击投票按钮而不选择单选按钮,那么我将看到消息*“您没有选择选项。”*

问题是上面我即使在选择一个选项时也会看到此消息。我该如何解决。

这些是polls / view.py

中的其余文件
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse



# Create your views here.

from .models import Choice,  Question 

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {  'latest_question_list': latest_question_list }
    return render( request, 'polls/index.html', context,);

def detail(request, question_id):
    try:
        question = get_object_or_404(Question, pk=question_id)
    except Question.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'polls/detail.html', {'question' : question})

def results(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/results.html', {'question': question})

这些是我的模板:

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

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>

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>

0 个答案:

没有答案