File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -105,3 +105,28 @@ def test_index_view_with_two_past_questions(self):
105
105
response .context ['latest_question_list' ],
106
106
['<Question: Past question 2.>' , '<Question: Past question 1.>' ]
107
107
)
108
+
109
+
110
+ class QuestionIndexDetailTests (TestCase ):
111
+ def test_detail_view_with_a_future_question (self ):
112
+ """
113
+ The detail view of a question with a pub_date in the future should
114
+ return a 404 not found.
115
+ """
116
+ future_question = create_question (question_text = 'Future question.' ,
117
+ days = 5 )
118
+ response = self .client .get (reverse ('polls:detail' ,
119
+ args = (future_question .id ,)))
120
+ self .assertEqual (response .status_code , 404 )
121
+
122
+ def test_detail_view_with_a_past_question (self ):
123
+ """
124
+ The detail view of a question with a pub_date in the past should
125
+ display the question's text.
126
+ """
127
+ past_question = create_question (question_text = 'Past Question.' ,
128
+ days = - 5 )
129
+ response = self .client .get (reverse ('polls:detail' ,
130
+ args = (past_question .id ,)))
131
+ self .assertContains (response , past_question .question_text ,
132
+ status_code = 200 )
Original file line number Diff line number Diff line change @@ -25,6 +25,12 @@ class DetailView(generic.DetailView):
25
25
model = Question
26
26
template_name = 'polls/detail.html'
27
27
28
+ def get_queryset (self ):
29
+ """
30
+ Excludes any questions that aren't published yet.
31
+ """
32
+ return Question .objects .filter (pub_date__lte = timezone .now ())
33
+
28
34
29
35
class ResultsView (generic .DetailView ):
30
36
model = Question
You can’t perform that action at this time.
0 commit comments