gpt4 book ai didi

python - IndexView 缺少 QuerySet。定义 IndexView.model - tutorial 4 django

转载 作者:行者123 更新时间:2023-11-28 21:14:22 26 4
gpt4 key购买 nike

我正在关注 this tutorial .在页面末尾,我不得不修改我的 views.py 和我的 urls.py

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

views.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'

当我尝试访问管理站点或投票应用程序时,我得到了这个:

ImproperlyConfigured at /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]

关于python - IndexView 缺少 QuerySet。定义 IndexView.model - tutorial 4 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31631847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com