diff --git a/djlearn/polls/admin.py b/djlearn/polls/admin.py index 9e7a581..ee4e60e 100644 --- a/djlearn/polls/admin.py +++ b/djlearn/polls/admin.py @@ -1,6 +1,7 @@ from django.contrib import admin -from .models import Question +from .models import Question, Choice # Register your models here. admin.site.register(Question) +admin.site.register(Choice) diff --git a/djlearn/polls/urls.py b/djlearn/polls/urls.py index 1e61ab4..3344b7c 100644 --- a/djlearn/polls/urls.py +++ b/djlearn/polls/urls.py @@ -3,8 +3,8 @@ from . import views app_name = 'polls' urlpatterns = [ - path('', views.index, name='index'), - path('/', views.detail, name='detail'), - path('/results/', views.results, name='results'), + path('', views.IndexView.as_view(), name='index'), + path('/', views.DetailView.as_view(), name='detail'), + path('/results/', views.ResultsView.as_view(), name='results'), path('/vote/', views.vote, name='vote'), ] diff --git a/djlearn/polls/views.py b/djlearn/polls/views.py index 12e3b00..e958ed9 100644 --- a/djlearn/polls/views.py +++ b/djlearn/polls/views.py @@ -1,26 +1,28 @@ -from django.shortcuts import render, get_object_or_404 -from django.http import HttpResponse, HttpResponseRedirect +from django.http import HttpResponseRedirect +from django.shortcuts import get_object_or_404, render from django.urls import reverse +from django.views import generic from .models import Choice, Question -# Create your views here. + +class IndexView(generic.ListView): + template_name = 'polls/index.html' + context_object_name = 'latest_question_list' + + def get_queryset(self): + """Return the last five published questions.""" + return Question.objects.order_by('-pub_date')[:5] -def index(request): - latest_question_list = Question.objects.order_by('-pub_date')[:5] - context = {'latest_question_list': latest_question_list} - return render(request, 'polls/index.html', context) +class DetailView(generic.DetailView): + model = Question + template_name = 'polls/detail.html' -def detail(request, question_id): - question = get_object_or_404(Question, pk=question_id) - return render(request, 'polls/detail.html', {'question': question}) - - -def results(request, question_id): - question = get_object_or_404(Question, pk=question_id) - return render(request, 'polls/results.html', {'question': question}) +class ResultsView(generic.DetailView): + model = Question + template_name = 'polls/results.html' def vote(request, question_id):