Django Polling App给出的错误为"No Polls are Available"



我搜索了所有过去的答案,但找不到线索。

在Django Polling应用程序教程中,我已经获得了通用视图。在定义了views.py、urls.py和模板(index.html、detail.htmls和results.html(之后,当我转到url localhost:8000/polls/i时,我得到了没有可用轮询的消息。如果我转到url localhost:8000/polls/1/I;有什么新功能"有选择的问题。

我在下面的文件:views.py

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from .models import Question, Choice
from django.urls import reverse
from django.views import generic
from django.utils import timezone
# Create your views here.

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'

def get_queryset(self):
"""
Excludes any questions that aren't published yet.
"""
return Question.objects.filter(pub_date__lte=timezone.now().date())

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:
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() 
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

urls.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'),
]

index.html

<!DOCTYPE html>
<html lang="en">
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{% if latest_questions_list %}
<ul>
{% for question in latest_questions_list %}
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available</p>
{% endif %}
</body>
</html>

我哪里错了?

在您的模板中有一个拼写错误,请将latest_questions_list更改为latest_question_list:

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

将其更改为:

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

应该能解决你的问题。

最新更新