Coverage for polls / views.py: 67%

30 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-21 13:48 +0900

1# -*- config: utf8 -*- 

2from django.shortcuts import render, get_object_or_404, get_list_or_404 

3from django.http import HttpResponse, HttpResponseRedirect 

4from django.urls import reverse 

5from django.utils import timezone 

6from django.views import generic 

7from .models import Question, Choice 

8 

9# Create your views here. 

10 

11#def index(request): 

12# latest_question_list = Question.objects.order_by('-pub_date')[:5] 

13# context = { 

14# 'latest_question_list': latest_question_list 

15# } 

16# return render(request, 'polls/index.html', context) 

17class IndexView(generic.ListView): 

18 '''質問の一覧を表示する汎用ビュー。 

19 ''' 

20 template_name = 'polls/index.html' 

21 '''質問一覧表示テンプレートのパス。''' 

22 context_object_name = 'latest_question_list' 

23 '''コンテキストのオブジェクト名。 

24 テンプレートで使用する変数名。テンプレートからこの変数名で質問の一覧を 

25 参照する。 

26 ''' 

27 

28 def get_queryset(self): 

29 '''Return the latest five published questions. 

30 Return the last five published questions (not including those set to be 

31 published in the future). 

32 ''' 

33 return Question.objects.filter( 

34 pub_date__lte=timezone.now() 

35 ).order_by('-pub_date')[:5] 

36 

37#def detail(request, question_id): 

38# question = get_object_or_404(Question, pk=question_id) 

39# context = {'question': question} 

40# return render(request, 'polls/detail.html', context) 

41class DetailView(generic.DetailView): 

42 '''詳細画面の汎用ビュー。 

43  

44 質問に対する回答の選択画面を表示する。 

45  

46 ''' 

47 model = Question 

48 template_name = 'polls/detail.html' 

49 

50 def get_queryset(self): 

51 ''' 

52 Excludes any questions that aren\'t published yet. 

53 ''' 

54 return Question.objects.filter(pub_date__lte=timezone.now()) 

55 

56#def results(request, question_id): 

57# question = get_object_or_404(Question, pk=question_id) 

58# return render(request, 'polls/results.html', {'question': question}) 

59class ResultsView(generic.DetailView): 

60 '''回答結果画面の汎用ビュー。 

61  

62 質問に対する回答のリストを表示し、それぞれの回答に対する投票数を表示する。 

63  

64 ''' 

65 model = Question 

66 template_name = 'polls/results.html' 

67 

68def vote(request, question_id): 

69 '''回答の選択結果を処理するビュー関数。 

70  

71 選択された回答の投票数を1つ増やして、その結果をChoiseモデルに保存する。 

72 その後、回答結果画面を表示する。 

73  

74 例外処理 

75 * リクエストで受け取った question_id が存在しない場合は 404 エラーを表示 

76 する。 

77 * リクエストで送信された回答が未設定のものだった場合は、詳細画面を表示 

78 してエラーメッセージを表示する。 

79  

80 ''' 

81 question = get_object_or_404(Question, pk=question_id) 

82 try: 

83 selected_choice = question.choice_set.get(pk=request.POST['choice']) 

84 

85 except (KeyError, Choice.DoesNotExist): 

86 # Redisplay the question voting form. 

87 return render(request, 'polls/detail.html', { 

88 'question': question, 

89 'error_message': 'You did\'nt select a choice.', 

90 }) 

91 else: 

92 selected_choice.votes += 1 

93 selected_choice.save() 

94 # Always return an HttpResponseRedirect after successfully dealing with 

95 # POST data. This prevents data from being posted twice if a user 

96 # hits Back button. 

97 return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))