首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django Polling App给出错误"No Polls of“

Django Polling App给出错误"No Polls of“
EN

Stack Overflow用户
提问于 2020-08-22 23:13:06
回答 1查看 143关注 0票数 0

我搜索了所有过去的答案,但没有得到任何线索。

在Django Polling App教程中,我介绍了通用视图。在定义了views.py、urls.py和模板(index.html、detail.htmls和results.html)之后,当我转到url localhost:8000/ polls / I时,收到消息no polls are available。如果我转到url localhost:8000/polls/1/,我可以看到"What's New?“有选择的问题。

下面是我的文件: views.py

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<!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>

我哪里错了?

EN

回答 1

Stack Overflow用户

发布于 2020-08-22 23:27:50

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

代码语言:javascript
复制
{% 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 %}

将其更改为:

代码语言:javascript
复制
{% 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 %}

应该能解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63537740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档