alpha
Login
or
Join now
shuuji3.xyz
/
django-tutorial
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/shuuji3/django-tutorial.
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
Pipelines
rewrite the view to use generic views
author
TAKAHASHI Shuuji
date
9 years ago
(Apr 6, 2017, 11:48 AM +0900)
commit
b9a15643
b9a15643809e7c14ea576df553c189a01c63f319
parent
a213a49e
a213a49e0ccdd087f50aded189d83633137a99c6
+39
-4
1 changed file
Expand all
Collapse all
Unified
Split
mysite
polls
views.py
+39
-4
mysite/polls/views.py
View file
Reviewed
···
1
1
-
from django.shortcuts import render
2
2
-
from django.http import HttpResponse
1
1
+
from django.shortcuts import get_object_or_404, render
2
2
+
from django.http import HttpResponseRedirect
3
3
+
from django.urls import reverse
4
4
+
from django.views import generic
5
5
+
6
6
+
from .models import Choice, Question
7
7
+
8
8
+
9
9
+
class IndexView(generic.ListView):
10
10
+
template_name = 'polls/index.html'
11
11
+
context_object_name = 'latest_question_list'
12
12
+
13
13
+
def get_queryset(self):
14
14
+
'''Return the last five published questions.'''
15
15
+
return Question.objects.order_by('-pub_date')[:5]
16
16
+
17
17
+
18
18
+
class DetailView(generic.DetailView):
19
19
+
model = Question
20
20
+
template_name = 'polls/detail.html'
21
21
+
22
22
+
23
23
+
class ResultsView(generic.DetailView):
24
24
+
model = Question
25
25
+
template_name = 'polls/results.html'
3
26
4
27
5
5
-
def index(request):
6
6
-
return HttpResponse("Hello, world. You're at the polls index.")
28
28
+
def vote(request, question_id):
29
29
+
question = get_object_or_404(Question, pk=question_id)
30
30
+
try:
31
31
+
selected_choice = question.choice_set.get(pk=request.POST['choice'])
32
32
+
except (KeyError, Choice.DoesNotExist):
33
33
+
# Redisplay the question voting form with error_message
34
34
+
return render(request, 'polls/detail.html', {
35
35
+
'question': question,
36
36
+
'error_message': "You didn't select a choice.",
37
37
+
})
38
38
+
else:
39
39
+
selected_choice.votes += 1
40
40
+
selected_choice.save()
41
41
+
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))