django tutorial #4 completed

This commit is contained in:
pedrolab 2020-07-20 02:09:01 +02:00
parent 0efde9e43c
commit 485e78a6e2
4 changed files with 84 additions and 23 deletions

View file

@ -1,6 +1,12 @@
<h1>{{ question.question_text }}</h1> <h1>{{ question.question_text }}</h1>
<ul>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %} {% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li> <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %} {% endfor %}
</ul> <input type="submit" value="Vote">
</form>

View file

@ -0,0 +1,9 @@
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

View file

@ -6,11 +6,12 @@ from . import views
app_name = 'polls' app_name = 'polls'
urlpatterns = [ urlpatterns = [
# ex: /polls/ # ex: /polls/
path('', views.index, name='index'), #path('', views.index, name='index'),
path('', views.IndexView.as_view(), name='index'),
# ex: /polls/5/ # ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'), path('<int:pk>/', views.DetailView.as_view(), name='detail'),
# ex: /polls/5/results/ # ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'), path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
# ex: /polls/5/vote/ # ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'), path('<int:question_id>/vote/', views.vote, name='vote'),
] ]

View file

@ -2,10 +2,12 @@ from django.shortcuts import get_object_or_404, render
# Create your views here. # Create your views here.
from django.http import HttpResponse from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader from django.template import loader
from django.urls import reverse
from django.views import generic
from .models import Question from .models import Choice, Question
from django.http import Http404 from django.http import Http404
@ -30,12 +32,20 @@ from django.http import Http404
## meaningful, templated and easy to write index ## meaningful, templated and easy to write index
## and then we no longer need to import loader and HttpResponse ## and then we no longer need to import loader and HttpResponse
def index(request): #def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5] # latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = { # context = {
'latest_question_list': latest_question_list, # 'latest_question_list': latest_question_list,
} # }
return render(request, 'polls/index.html', context) # return render(request, 'polls/index.html', context)
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_question_list'
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
## simple detail ## simple detail
#def detail(request, question_id): #def detail(request, question_id):
@ -50,13 +60,48 @@ def index(request):
# return render(request, 'polls/detail.html', {'question': question}) # return render(request, 'polls/detail.html', {'question': question})
## meaningful and easy to write detail ## meaningful and easy to write detail
def detail(request, question_id): #def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id) # question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question}) # return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id): ## refactor detail view
response = "You're looking at the results of question %s." class DetailView(generic.DetailView):
return HttpResponse(response % question_id) model = Question
template_name = 'polls/detail.html'
#def results(request, question_id):
# response = "You're looking at the results of question %s."
# return HttpResponse(response % question_id)
# # real implementation of results
#def results(request, question_id):
# question = get_object_or_404(Question, pk=question_id)
# return render(request, 'polls/results.html', {'question': question})
# # refactor results view
class ResultsView(generic.DetailView):
model = Question
template_name = 'polls/results.html'
#def vote(request, question_id):
# return HttpResponse("You're voting on question %s." % question_id)
# #real implementation of vote
def vote (request, question_id): def vote (request, question_id):
return HttpResponse("You're voting on question %s." % 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,)))