From 25bfca067c7c1dcbe15c5a7189a09bbb1ed7808b Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Sun, 6 May 2018 20:21:35 -0700 Subject: [PATCH] django: finish tutorial #3 --- .../mysite/polls/templates/polls/detail.html | 6 +++++ .../mysite/polls/templates/polls/index.html | 9 ++++++++ django/mysite/polls/urls.py | 4 ++++ django/mysite/polls/views.py | 23 ++++++++++++++++--- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 django/mysite/polls/templates/polls/detail.html create mode 100644 django/mysite/polls/templates/polls/index.html diff --git a/django/mysite/polls/templates/polls/detail.html b/django/mysite/polls/templates/polls/detail.html new file mode 100644 index 0000000..e38ffba --- /dev/null +++ b/django/mysite/polls/templates/polls/detail.html @@ -0,0 +1,6 @@ +

{{ question.question_text }}

+ \ No newline at end of file diff --git a/django/mysite/polls/templates/polls/index.html b/django/mysite/polls/templates/polls/index.html new file mode 100644 index 0000000..8f6b1b3 --- /dev/null +++ b/django/mysite/polls/templates/polls/index.html @@ -0,0 +1,9 @@ +{% if latest_questions %} + +{% else %} +

No polls are available.

+{% endif %} \ No newline at end of file diff --git a/django/mysite/polls/urls.py b/django/mysite/polls/urls.py index 88a9cac..293729b 100644 --- a/django/mysite/polls/urls.py +++ b/django/mysite/polls/urls.py @@ -2,6 +2,10 @@ from django.urls import path from . import views +app_name = 'polls' urlpatterns = [ path('', views.index, name='index'), + path('/', views.detail, name='detail'), + path('/results/', views.results, name='results'), + path('/vote/', views.vote, name='vote'), ] diff --git a/django/mysite/polls/views.py b/django/mysite/polls/views.py index 34c2c6b..4ae3f90 100644 --- a/django/mysite/polls/views.py +++ b/django/mysite/polls/views.py @@ -1,6 +1,23 @@ -from django.shortcuts import render -from django.http import HttpResponse +from django.http import HttpResponse, Http404 +from django.shortcuts import get_object_or_404, render + +from . import models + # Create your views here. def index(request): - return HttpResponse("Hello, world. You're at the polls index.") + latest_questions = models.Question.objects.order_by('-pub_date')[:5] + ctx = {'latest_questions': latest_questions} + return render(request, 'polls/index.html', ctx) + +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) + +def vote(request, question_id): + return HttpResponse("You're voting on {}".format(question_id)) +