From 99b8f0681378f83b8e2f9d5a8b1736211da72e91 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sun, 6 May 2018 22:09:29 -0700 Subject: [PATCH] django: mysite/working on tutorial 4 during switch to generic views, blah blah blah --- .../mysite/polls/templates/polls/detail.html | 17 +++++-- django/mysite/polls/urls.py | 6 +-- django/mysite/polls/views.py | 50 +++++++++++++------ django/mysite/progress.txt | 2 + 4 files changed, 52 insertions(+), 23 deletions(-) diff --git a/django/mysite/polls/templates/polls/detail.html b/django/mysite/polls/templates/polls/detail.html index e38ffba..63c4259 100644 --- a/django/mysite/polls/templates/polls/detail.html +++ b/django/mysite/polls/templates/polls/detail.html @@ -1,6 +1,13 @@

{{ question.question_text }}

- \ No newline at end of file + +{% if error_message %}

{{ error_message }}

{% endif %} + +
+{% csrf_token %} +{% for choice in question.choice_set.all %} + +
+{% endfor %} + + +
\ No newline at end of file diff --git a/django/mysite/polls/urls.py b/django/mysite/polls/urls.py index 293729b..eff2be0 100644 --- a/django/mysite/polls/urls.py +++ b/django/mysite/polls/urls.py @@ -4,8 +4,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/django/mysite/polls/views.py b/django/mysite/polls/views.py index 4ae3f90..16f582c 100644 --- a/django/mysite/polls/views.py +++ b/django/mysite/polls/views.py @@ -1,23 +1,43 @@ -from django.http import HttpResponse, Http404 from django.shortcuts import get_object_or_404, render +from django.http import HttpResponseRedirect +from django.urls import reverse +from django.views import generic -from . import models +from .models import Choice, Question + +class IndexView(generic.ListView): + template_name = 'polls/index.html' + context_object_name = 'latest_questions' + + def get_queryset(self): + """Returns the last five published questions.""" + return Question.objects.order_by('-pub_date')[:5] -# Create your views here. -def index(request): - latest_questions = models.Question.objects.order_by('-pub_date')[:5] - ctx = {'latest_questions': latest_questions} - return render(request, 'polls/index.html', ctx) +class DetailView(generic.DetailView): + model = Question + template_name = 'polls/detail.html' -def detail(request, question_id): - q = get_object_or_404(models.Question, pk=question_id) - return render(request, 'polls/detail.html', {'question': q}) -def results(request, question_id): - response = "Tally for \"{}\":".format(question_id) - return HttpResponse(response) +class ResultsView(generic.DetailView): + model = Choice + template_name = 'polls/results.html' + def vote(request, question_id): - return HttpResponse("You're voting on {}".format(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,))) \ No newline at end of file diff --git a/django/mysite/progress.txt b/django/mysite/progress.txt index 54235c3..23a19a0 100644 --- a/django/mysite/progress.txt +++ b/django/mysite/progress.txt @@ -1 +1,3 @@ https://docs.djangoproject.com/en/2.0/intro/tutorial03/ +https://docs.djangoproject.com/en/2.0/intro/tutorial04/ +