No Vote, but Error Page Does Not Appear

Asked 2 years ago, Updated 2 years ago, 73 views

Prerequisites/What you want to achieve
I'm working on a tutorial for Django.To create a voting application, visit the following site:

https://docs.djangoproject.com/ja/4.0/intro/tutorial04/

If you do not vote, an error page should appear, but it does not appear.

Affected Source Code
pols /views.py

 from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.urls import reverse

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):
    question=get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question':question})

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

defvote(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,)))

pols /urls.py

 from django.urls import path
from.import views

app_name = 'polls'
urlpatterns = [
    path(', views.index, name='index',
    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results/', views.results, name='results',
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

pols/template/polls/index.html

{%iflatest_question_list%}
    <ul>
    {% for question in latest_question_list%}
        <li><a href="{%url'polls:detail'question.id%}">{{question.question_text}}</a></li>;
    </ul>
{% else%}
    <p>No polls are available.</p>
{% endif%}

pols/template/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>

pols/template/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>

python django

2022-09-30 19:38

1 Answers

For the time being, the following is the result of missing work and copying an old article.
These minor leaks and mistakes can lead to problems, so please check carefully.

views.py—Insufficient import on the first line.
Wrong:

 from django.http import HttpResponse

positive:

 from django.http import HttpResponse, HttpResponseRedirect

Polls/template/polls/index.html: Missing input on previous page of Tutorial ({%endfor%}).
Wrong:

<ul>
    {% for question in latest_question_list%}
        <li><a href="{%url'polls:detail'question.id%}">{{question.question_text}}</a></li>;
    </ul>

positive:

<ul>
    {% for question in latest_question_list%}
        <li><a href="{%url'polls:detail'question.id%}">{{question.question_text}}</a></li>;
    {% endfor%}
    </ul>

pols/template/polls/detail.html—The content of the article that you worked on in the older version of Tutorial that was introduced in the comment is used.
Wrong:

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

positive:

<form action="{%url'polls:vote'question.id%}" method="post">
{% csrf_token%}
<fieldset>
    <legend><h1>{{question.question_text}}</h1></legend>
    {% if error_message%}<p>strong>{{error_message}}</strong>/p>{%endif%}
    {% 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%}
</fieldset>
<input type="submit" value="Vote">
</form>


2022-09-30 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.