[英]django 1.8:XView is missing a QuerySet. Define XView.model, XView.queryset,
[英]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.