finished tut4, onto tut 5

This commit is contained in:
Anish Lakhwara 2020-07-07 22:14:26 +10:00
parent ac1f869c94
commit 16b38b0d24
15 changed files with 22 additions and 5 deletions

View file

@ -1,7 +1,7 @@
{% if question_list %}
<ul>
{% for question in question_list %}
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}

View file

@ -4,8 +4,8 @@ from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.result, name='result'),
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='result'),
path('<int:question_id>/vote/', views.vote, name='vote')
]

View file

@ -2,9 +2,26 @@ from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import loader
from django.urls import reverse
from .models import Question
from django.views import generic
from .models import Question, Choice
# Create your views here.
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'question_list'
def get_queryset(self):
# Return last 5 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/resulthtml'
def index(request):
question_list = Question.objects.order_by('-pub_date')[:5]
context = { 'question_list': question_list }