move functions to class
Used generic views
This commit is contained in:
@@ -4,11 +4,8 @@ from . import views
|
|||||||
|
|
||||||
app_name = "polls"
|
app_name = "polls"
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.index, name="index"),
|
path("", views.IndexView.as_view(), name="index"),
|
||||||
# example: /polls/5/
|
path("<int:pk>/", views.DetailView.as_view(), name="detail"),
|
||||||
path("<int:question_id>/", views.detail, name="detail"),
|
path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
|
||||||
# example: /polls/5/results/
|
|
||||||
path("<int:question_id>/results/", views.results, name="results"),
|
|
||||||
# example: /polls/5/vote/
|
|
||||||
path("<int:question_id>/vote/", views.vote, name="vote"),
|
path("<int:question_id>/vote/", views.vote, name="vote"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,8 +1,28 @@
|
|||||||
|
from django.db.models import F
|
||||||
from django.shortcuts import render, get_object_or_404
|
from django.shortcuts import render, get_object_or_404
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse, HttpResponseRedirect
|
||||||
from django.template import loader
|
from django.template import loader
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.views import generic
|
||||||
|
|
||||||
from .models import Question
|
from .models import Question, Choice
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
class DetailView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = "polls/detail.html"
|
||||||
|
|
||||||
|
|
||||||
|
class ResultsView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = "polls/results.html"
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
latest_question_list = Question.objects.order_by("-pub_date")[:5]
|
latest_question_list = Question.objects.order_by("-pub_date")[:5]
|
||||||
@@ -14,8 +34,27 @@ def detail(request, question_id):
|
|||||||
return render(request, "polls/detail.html", {"question": question})
|
return render(request, "polls/detail.html", {"question": question})
|
||||||
|
|
||||||
def results(request, question_id):
|
def results(request, question_id):
|
||||||
response = "You're looking at the results of question %s."
|
question = get_object_or_404(Question, pk=question_id)
|
||||||
return HttpResponse(response % question_id)
|
return render(request, "polls/results.html", {"question": question})
|
||||||
|
|
||||||
def vote(request, question_id):
|
def vote(request, question_id):
|
||||||
return HttpResponse("You're voting on question %s." % question_id)
|
question = get_object_or_404(Question, pk=question_id)
|
||||||
|
try:
|
||||||
|
selected_choice = question.choice_set.get(pk=request.POST["choice"])
|
||||||
|
except (KeyError, Choice.DoesNotExist):
|
||||||
|
# Redisplay the question voting form.
|
||||||
|
return render(
|
||||||
|
request,
|
||||||
|
"polls/detail.html",
|
||||||
|
{
|
||||||
|
"question": question,
|
||||||
|
"error_message": "You didn't select a choice.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
selected_choice.votes = F("votes") + 1
|
||||||
|
selected_choice.save()
|
||||||
|
# Always return an HttpResponseRedirect after successfully dealing
|
||||||
|
# with POST data. This prevents data from being posted twice if a
|
||||||
|
# user hits the Back button.
|
||||||
|
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
||||||
|
|||||||
Reference in New Issue
Block a user