dj-ankieta/polls/views.py
2019-06-14 20:41:55 +02:00

107 lines
3.7 KiB
Python

from django.shortcuts import get_object_or_404, render
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from django.views import generic
# from django.http import Http404
# from django.template import loader
from django.utils import timezone
from .models import Choice, Question
# def index(request):
# return HttpResponse("Hello, world. You're at the polls index.")
# def detail(request, question_id):
# return HttpResponse("Youre looking at question %s." % question_id)
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryrset(self):
"""Return last 5 published polls, not counting future ones"""
return Question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5]
class DetailView(generic.DetailView):
model = Question
template_name = 'polls/detail.html'
def get_queryrset(self):
"""
excludes questions not yet published
"""
return Question.objects.filter(pub_date__lte=timezone.now())
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
def results(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/results.html', {'question': question})
# response = "Your looking at the results of question %s."
# return HttpResponse(response % question_id)
def votes(request, question_id):
return HttpResponse("You are voting on %s." % question_id)
def index(request):
last_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'last_question_list': last_question_list}
return render(request, 'polls/index.html', context)
# output = ', '.join([q.question_text for q in last_question_list])
# return HttpResponse(output)
# context = {'last_question_list': last_question_list}
# return render(request, 'polls/index.html', context)
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
# doing this new short cut, we don't see the custom wording, how do we do
# that?
# try:
# question = Question.objects.get(pk=question_id)
# except Question.DoesNotExist:
# raise Http404("Question does not exist")
# return render(request, 'polls/detail.html', {'question': question})
def vote(request, 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 += 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,)))
# return HttpResponse("Your voting on question %s." % question_id)
# def detail(request, question_id):
# try:
# question = Question.objects.get(pk=question_id)
# except Question.DoesNotExist:
# raise Http404("question does not exist")
# return render(request, 'polls/detal.html', 'question': question