Skip to content

Commit 75d863b

Browse files
committed
Finish part 4 of Django tutorial
1 parent d1f75ec commit 75d863b

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

mysite/polls/templates/polls/results.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ <h1>{{ question.question_text }}</h1>
66
{% endfor %}
77
</ul>
88

9-
<a href="{% url 'polls:fetail' question.id %}">Vote Again?</a>
9+
<a href="{% url 'polls:detail' question.id %}">Vote Again?</a>

mysite/polls/urls.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from . import views
44

5-
#sets the application namespace
5+
# sets the application namespace
66
app_name = "polls"
77
# this is a URLconf
88
urlpatterns = [
9-
path("", views.index, name="index"),
10-
path("<int:question_id>/", views.detail, name="detail"),
11-
path("<int:question_id>/results/", views.results, name="results"),
9+
path("", views.IndexView.as_view(), name="index"),
10+
path("<int:pk>/", views.DetailView.as_view(), name="detail"),
11+
path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
1212
path("<int:question_id>/vote/", views.vote, name="vote"),
1313
]

mysite/polls/views.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,36 @@
22
# and template loader
33
from django.shortcuts import get_object_or_404, render
44

5-
from django.http import HttpResponse, HttpResponseRedirect
5+
from django.http import HttpResponseRedirect
66
from django.urls import reverse
7+
from django.views import generic
78

89
from .models import Choice, Question
910

1011

1112
# Create your views here.
12-
def index(request):
13-
# Grab the 5 most recent questions, ordered by with
14-
# the most reecnt first
15-
latest_question_list = Question.objects.order_by("-pub_date")[:5]
16-
context = {
17-
"latest_question_list": latest_question_list
18-
}
19-
return render(request, "polls/index.html", context)
13+
class IndexView(generic.ListView):
14+
# Overriding the the ListView generic view default
15+
# template called <app name>/<model name>_list.html
16+
template_name = "polls/index.html"
17+
# Override the automatically generated context variable is question_list
18+
context_object_name = "latest_question_list"
2019

20+
def get_queryset(self):
21+
"""Return the last five pulished questions."""
22+
return Question.objects.order_by("-pub_date")[:5]
2123

22-
def detail(request, question_id):
23-
question = get_object_or_404(Question, pk=question_id)
24-
return render(request, "polls/detail.html", {"question": question})
2524

25+
class DetailView(generic.DetailView):
26+
model = Question
27+
# Overrides the default template name of DetailView
28+
# generic, default is <app name>/<model name>_detail.html
29+
template_name = "polls/detail.html"
2630

27-
def results(request, question_id):
28-
question = get_object_or_404(Question, pk=question_id)
29-
return render(request, "polls/results.html", {"question": question})
31+
32+
class ResultsView(generic.DetailView):
33+
model = Question
34+
template_name = "polls/results.html"
3035

3136

3237
def vote(request, question_id):
@@ -48,5 +53,5 @@ def vote(request, question_id):
4853
# Always return an HttpResponseRedirect after successfully dealing
4954
# with POST data. This prevents data from being posted twice if a
5055
# user hits the Back button.
51-
url_redirect = "poll:results"
56+
url_redirect = "polls:results"
5257
return HttpResponseRedirect(reverse(url_redirect, args=(question_id,)))

0 commit comments

Comments
 (0)