2
2
from django .core .urlresolvers import reverse
3
3
from django .http import HttpResponseRedirect
4
4
from django .shortcuts import render , get_object_or_404
5
+ from django .views .generic import ListView , DetailView
6
+
5
7
from polls .models import Question , Choice
6
8
9
+ # logging 추가
10
+ import logging
11
+ logger = logging .getLogger (__name__ )
12
+
13
+ #-- Class-based GenericView
14
+ class IndexView (ListView ):
15
+ template_name = 'polls/index.html'
16
+ context_object_name = 'latest_question_list'
17
+
18
+ def get_queryset (self ):
19
+ '''Return the last five published questions.'''
20
+ return Question .objects .order_by ('-pub_date' )[:5 ]
7
21
8
- def index (request ):
9
- latest_question_list = Question .objects .all ().order_by ('-pub_date' )[:5 ]
10
- context = {'latest_question_list' : latest_question_list }
11
- return render (request , 'polls/index.html' , context )
12
22
23
+ class DetailView (DetailView ):
24
+ model = Question
25
+ template_name = 'polls/detail.html'
13
26
14
- def detail (request , question_id ):
15
- question = get_object_or_404 (Question , pk = question_id )
16
- return render (request , 'polls/detail.html' , {'question' : question })
17
27
28
+ class ResultsView (DetailView ):
29
+ model = Question
30
+ template_name = 'polls/results.html'
18
31
32
+
33
+ #-- Function-based View
19
34
def vote (request , question_id ):
35
+ logger .debug ('vote().question_id: %s' % question_id ) # 추가
20
36
p = get_object_or_404 (Question , pk = question_id )
21
37
try :
22
38
selected_choice = p .choice_set .get (pk = request .POST ['choice' ])
@@ -31,9 +47,4 @@ def vote(request, question_id):
31
47
selected_choice .save ()
32
48
# POST 데이터를 정상적으로 처리하였으면,
33
49
# 항상 HttpResponseRedirect를 반환하여 리다이렉션 처리함
34
- return HttpResponseRedirect (reverse ('polls:results' , args = (p .id ,)))
35
-
36
-
37
- def results (request , question_id ):
38
- question = get_object_or_404 (Question , pk = question_id )
39
- return render (request , 'polls/results.html' , {'question' : question })
50
+ return HttpResponseRedirect (reverse ('polls:results' , args = (p .id ,)))
0 commit comments