Skip to content

Commit fe9ecb6

Browse files
committed
Add logger
1 parent dd9a10f commit fe9ecb6

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

mysite/settings.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,30 @@
8383

8484
STATIC_URL = '/static/'
8585
# TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
86-
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
86+
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
87+
88+
# logging
89+
LOGGING = {
90+
'version': 1,
91+
'disable_existing_loggers': False,
92+
'formatters': {
93+
'verbose': {
94+
'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
95+
'datefmt': "%d/%b/%Y %H:%M:%S"
96+
},
97+
},
98+
'handlers': {
99+
'file': {
100+
'level': 'DEBUG',
101+
'class': 'logging.FileHandler',
102+
'filename': os.path.join(BASE_DIR, 'logs/logfile'),
103+
'formatter': 'verbose'
104+
},
105+
},
106+
'loggers': {
107+
'polls': {
108+
'handlers': ['file'],
109+
'level': 'DEBUG',
110+
},
111+
}
112+
}

polls/views.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,37 @@
22
from django.core.urlresolvers import reverse
33
from django.http import HttpResponseRedirect
44
from django.shortcuts import render, get_object_or_404
5+
from django.views.generic import ListView, DetailView
6+
57
from polls.models import Question, Choice
68

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]
721

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)
1222

23+
class DetailView(DetailView):
24+
model = Question
25+
template_name = 'polls/detail.html'
1326

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})
1727

28+
class ResultsView(DetailView):
29+
model = Question
30+
template_name = 'polls/results.html'
1831

32+
33+
#-- Function-based View
1934
def vote(request, question_id):
35+
logger.debug('vote().question_id: %s' % question_id) # 추가
2036
p = get_object_or_404(Question, pk=question_id)
2137
try:
2238
selected_choice = p.choice_set.get(pk=request.POST['choice'])
@@ -31,9 +47,4 @@ def vote(request, question_id):
3147
selected_choice.save()
3248
# POST 데이터를 정상적으로 처리하였으면,
3349
# 항상 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

Comments
 (0)