簡體   English   中英

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

[英]IndexView is missing a QuerySet. Define IndexView.model - tutorial 4 django

我正在關注本教程 在頁面的末尾,我不得不修改我的 views.py 和我的 urls.py

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

視圖.py:

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'

當我嘗試訪問管理站點或投票應用程序時,我得到了這個:

在 /polls/ 配置不當

IndexView is missing a QuerySet. Define IndexView.model, IndexView.queryset, or
override IndexView.get_queryset().

Request Method:     GET
Request URL:    http://127.0.0.1:8000/polls/
Django Version:     1.8.3
Exception Type:     ImproperlyConfigured
Exception Value:    

IndexView is missing a QuerySet. Define IndexView.model, IndexView.queryset, or
override IndexView.get_queryset().

Exception Location:     /usr/local/lib/python2.7/dist-packages/
Django-1.8.3-py2.7.egg/django/views/generic/list.py in get_queryset, line 44

Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

['/home/eddy/Documentos/django/mysite',
 '/home/eddy/.local/lib/python2.7/site-packages/setuptools-18.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/setuptools-18.0.1-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/virtualenv-13.1.0-py2.7.egg',
 '/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/eddy/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

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]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM