Hello, I am posting because I am working on the Django documentation
tutorials and am currently on the testing portion
(https://docs.djangoproject.com/en/2.2/intro/tutorial05/).
In this tutorial where we are building the polls application, there is a
portion where we wish to restrict what pages will render a 404 error based
on certain conditions, such as whether the the pole has a past or future
publication date. This is done using the get_queryset() function. They do
this for both the index page and the detail page.
I assume most people answering this question would be familiar and have
worked through this tutorial. What I am trying to do is prevent polls
showing up on the index page or having a detail page based on how many
choice options they have (obviously, there is no point of showing a poll
with 0 or 1 options).
Currently my code looks like this in polls/view.py
#################################################
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic # for the generic views
from django.utils import timezone
from .models import Choice, Question
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
#def get_queryset(self):
# return Question.objects.order_by('-pub_date')[:5]
def get_queryset(self):
queryset =
Question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5]
queryset = [question for question in queryset if
question.choice_set.count() >=2]
return queryset
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
def get_queryset(self):
"""
Excludes any questions that aren't published yet or have zero
choices.
"""
queryset = Question.objects.filter(pub_date__lte=timezone.now(), )
#queryset = [question for question in queryset if
question.choice_set.count() >=2]
return queryset
##################################################
The issue is this code works perfectly fine for the index page, but when I
uncomment the line in the get_queryset() function in the DetailView, I
obtain this error for any question.
###################################################
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/1/
Django Version: 2.2.3
Python Version: 3.6.3
Installed Applications:
['polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File
"/home/temporary/Desktop/Django/projects/tut_proj_1/lib/python3.6/site-packages/django/core/handlers/exception.py"
in inner
34. response = get_response(request)
File
"/home/temporary/Desktop/Django/projects/tut_proj_1/lib/python3.6/site-packages/django/core/handlers/base.py"
in _get_response
115. response = self.process_exception_by_middleware(e,
request)
File
"/home/temporary/Desktop/Django/projects/tut_proj_1/lib/python3.6/site-packages/django/core/handlers/base.py"
in _get_response
113. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File
"/home/temporary/Desktop/Django/projects/tut_proj_1/lib/python3.6/site-packages/django/views/generic/base.py"
in view
71. return self.dispatch(request, *args, **kwargs)
File
"/home/temporary/Desktop/Django/projects/tut_proj_1/lib/python3.6/site-packages/django/views/generic/base.py"
in dispatch
97. return handler(request, *args, **kwargs)
File
"/home/temporary/Desktop/Django/projects/tut_proj_1/lib/python3.6/site-packages/django/views/generic/detail.py"
in get
106. self.object = self.get_object()
File
"/home/temporary/Desktop/Django/projects/tut_proj_1/lib/python3.6/site-packages/django/views/generic/detail.py"
in get_object
36. queryset = queryset.filter(pk=pk)
Exception Type: AttributeError at /polls/1/
Exception Value: 'list' object has no attribute 'filter'
#######################################################
I am still new to this and I assume it may be due to the difference between
the generic Listview and the generic DetailView, or some issue with how the
content of the html templates polls/detail.html or polls/index.html is
rendered but I have no previous experience with html and only understand
Python in principle without working on any previous big projects like this.
Any help would be appreciated.
James
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/a9d794d0-e8d4-43ac-bf2f-5aa7f4078098%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.