Skip to content

Commit b18aa7b

Browse files
committed
Specify return type of get_queryset
1 parent 61a147c commit b18aa7b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

polls/tests.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,26 @@ def test_index_view_with_two_past_questions(self):
101101
self.assertQuerysetEqual(
102102
response.context['latest_question_list'],
103103
['<Question: Past question 2.>', '<Question: Past question 1.>']
104-
)
104+
)
105+
106+
107+
class QuestionIndexDetailTests(TestCase):
108+
def test_detail_view_with_a_future_question(self):
109+
"""
110+
The detail view of a question with a pub_date in the future should
111+
return a 404 not found.
112+
"""
113+
future_question = create_question(question_text='Future question.', days=5)
114+
url = reverse('polls:detail', args=(future_question.id,))
115+
response = self.client.get(url)
116+
self.assertEqual(response.status_code, 404)
117+
118+
def test_detail_view_with_a_past_question(self):
119+
"""
120+
The detail view of a question with a pub_date in the past should
121+
display the question's text.
122+
"""
123+
past_question = create_question(question_text='Past Question.', days=-5)
124+
url = reverse('polls:detail', args=(past_question.id,))
125+
response = self.client.get(url)
126+
self.assertContains(response, past_question.question_text)

polls/views.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from django.db.models.query import QuerySet
2+
13
from django.http import HttpResponseRedirect, HttpRequest, HttpResponse
24
from django.shortcuts import render, get_object_or_404
35
from django.urls import reverse
@@ -11,7 +13,7 @@ class IndexView(generic.ListView):
1113
template_name = 'polls/index.html'
1214
context_object_name = 'latest_question_list'
1315

14-
def get_queryset(self):
16+
def get_queryset(self) -> QuerySet:
1517
"""
1618
Return the last five published questions (not including those set to be
1719
published in the future).
@@ -24,6 +26,12 @@ class DetailView(generic.DetailView):
2426
model = Question
2527
template_name = 'polls/detail.html'
2628

29+
def get_queryset(self) -> QuerySet:
30+
"""
31+
Excludes any questions that aren't published yet.
32+
"""
33+
return Question.objects.filter(pub_date__lte=timezone.now())
34+
2735

2836
class ResultsView(generic.DetailView):
2937
model = Question

0 commit comments

Comments
 (0)