I'm following this tutorial. At the end of the page I had to modify my views.py and my 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'
When I try to access to the admin site or the poll app I got this:
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
should be
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]
Indentation matters. My guess is that your IndexView
is
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With