IndexView缺少QuerySet。定义IndexView.model - 教程4 django

时间:2015-07-25 22:44:35

标签: python django

我跟随this tutorial。在页面的最后我必须修改我的views.py和我的urls.py

urls.py:

Meteor.publish('pub-name', function(userInput){
   var firstLetters = new RegExp('^' + userInput);
   return Cities.find({name:firstLetters},{limit:10,sort:{name:1}});
});

views.py:

from django.conf.urls import url
from . import views

urlpatterns = [
               url(r'^$', views.IndexView.as_view(), name='index'),
               url(r'^(?P<pk>[0-9]+)/$',
                   views.DetailView.as_view(), name='detail'),
               url(r'^(?P<pk>[0-9]+)/results/$',
                   views.ResultsView.as_view(), name='results'),
               url(r'^(?P<question_id>[0-9]+)/vote/$',
               views.vote, name='vote'),
              ]

当我尝试访问管理网站或民意调查应用时,我得到了这个:

  

在/ polls /

处配置不当
from django.shortcuts import get_object_or_404, render 
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Choice, Question

class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

# SebasSBM's note: following the answer below, I assume that this method
#                  was wrongly identated like this, in the original case
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'

class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

2 个答案:

答案 0 :(得分:6)

IndexView应该是

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]

缩进很重要。我的猜测是你的IndexView

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]

答案 1 :(得分:-1)

//你必须在index.html中使用相同的context_object_name {{latest.question.list}}