From 4faefebe47da399f4a47c2002c87075b8bd2db53 Mon Sep 17 00:00:00 2001 From: Lukas Date: Fri, 27 Aug 2021 17:27:16 +0200 Subject: [PATCH] Part 3 Tutorial Done --- djlearn/polls/templates/polls/detail.html | 6 ++++++ djlearn/polls/templates/polls/index.html | 9 +++++++++ djlearn/polls/urls.py | 6 +++++- djlearn/polls/views.py | 21 +++++++++++++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 djlearn/polls/templates/polls/detail.html create mode 100644 djlearn/polls/templates/polls/index.html diff --git a/djlearn/polls/templates/polls/detail.html b/djlearn/polls/templates/polls/detail.html new file mode 100644 index 0000000..4e1bd67 --- /dev/null +++ b/djlearn/polls/templates/polls/detail.html @@ -0,0 +1,6 @@ +

{{ question.question_text }}

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

No polls are available.

+{% endif %} diff --git a/djlearn/polls/urls.py b/djlearn/polls/urls.py index 9a62f74..1e61ab4 100644 --- a/djlearn/polls/urls.py +++ b/djlearn/polls/urls.py @@ -1,6 +1,10 @@ from django.urls import path from . import views +app_name = 'polls' urlpatterns = [ - path("", views.index, name="index") + 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/djlearn/polls/views.py b/djlearn/polls/views.py index 18923c8..9d42a1c 100644 --- a/djlearn/polls/views.py +++ b/djlearn/polls/views.py @@ -1,8 +1,25 @@ -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse +from .models import Question # Create your views here. def index(request): - return HttpResponse("Hello World. This is the Polls index") + latest_question_list = Question.objects.order_by('-pub_date')[:5] + context = {'latest_question_list': latest_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}) + + +def results(request, question_id): + response = f"You're looking @ the results of question {question_id}" + return HttpResponse(response) + + +def vote(request, question_id): + return HttpResponse(f"You're voting on question {question_id}")