File tree Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Expand file tree Collapse file tree 2 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -101,4 +101,26 @@ def test_index_view_with_two_past_questions(self):
101
101
self .assertQuerysetEqual (
102
102
response .context ['latest_question_list' ],
103
103
['<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 )
Original file line number Diff line number Diff line change
1
+ from django .db .models .query import QuerySet
2
+
1
3
from django .http import HttpResponseRedirect , HttpRequest , HttpResponse
2
4
from django .shortcuts import render , get_object_or_404
3
5
from django .urls import reverse
@@ -11,7 +13,7 @@ class IndexView(generic.ListView):
11
13
template_name = 'polls/index.html'
12
14
context_object_name = 'latest_question_list'
13
15
14
- def get_queryset (self ):
16
+ def get_queryset (self ) -> QuerySet :
15
17
"""
16
18
Return the last five published questions (not including those set to be
17
19
published in the future).
@@ -24,6 +26,12 @@ class DetailView(generic.DetailView):
24
26
model = Question
25
27
template_name = 'polls/detail.html'
26
28
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
+
27
35
28
36
class ResultsView (generic .DetailView ):
29
37
model = Question
You can’t perform that action at this time.
0 commit comments