|
1 |
| -from django.http import HttpResponse, HttpResponseRedirect |
2 |
| -from polls.models import Choice, Question |
3 |
| -from django.shortcuts import render, get_object_or_404 |
| 1 | +from django.shortcuts import get_object_or_404, render |
| 2 | +from django.http import HttpResponseRedirect |
4 | 3 | from django.core.urlresolvers import reverse
|
| 4 | +from django.views import generic |
5 | 5 |
|
| 6 | +from .models import Choice, Question |
6 | 7 |
|
7 |
| -# Create your views here. |
8 |
| -def index(request): |
9 |
| - latest_question_list = Question.objects.order_by('-pub_date')[:5] |
10 |
| - context = {'latest_question_list': latest_question_list} |
11 |
| - return render(request, 'polls/index.html', context) |
12 | 8 |
|
| 9 | +class IndexView(generic.ListView): |
| 10 | + template_name = 'polls/index.html' |
| 11 | + context_object_name = 'latest_question_list' |
13 | 12 |
|
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}) |
| 13 | + def get_queryset(self): |
| 14 | + """Return the last five published questions.""" |
| 15 | + return Question.objects.order_by('-pub_date')[:5] |
17 | 16 |
|
18 | 17 |
|
19 |
| -def results(request, question_id): |
20 |
| - question = get_object_or_404(Question, pk=question_id) |
21 |
| - return render(request, 'polls/results.html', {'question': question}) |
| 18 | +class DetailView(generic.DetailView): |
| 19 | + model = Question |
| 20 | + template_name = 'polls/detail.html' |
| 21 | + |
| 22 | + |
| 23 | +class ResultsView(generic.DetailView): |
| 24 | + model = Question |
| 25 | + template_name = 'polls/results.html' |
22 | 26 |
|
23 | 27 |
|
24 | 28 | def vote(request, question_id):
|
|
0 commit comments