mirror of
https://github.com/Sosokker/ku-polls.git
synced 2025-12-20 14:04:05 +01:00
Apply minimal form to detail page/ Use generic view on index-detail-result
This commit is contained in:
parent
a62096f7e5
commit
f7c2012118
@ -1,6 +1,12 @@
|
|||||||
<h1>{{ question.question_text }}</h1>
|
<form action="{% url 'polls:vote' question.id %}" method="post">
|
||||||
<ul>
|
{% csrf_token %}
|
||||||
|
<fieldset>
|
||||||
|
<legend><h1>{{ question.question_text }}</h1></legend>
|
||||||
|
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
|
||||||
{% 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>
|
</fieldset>
|
||||||
|
<input type="submit" value="Vote">
|
||||||
|
</form>
|
||||||
9
polls/templates/polls/result.html
Normal file
9
polls/templates/polls/result.html
Normal 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>
|
||||||
@ -4,8 +4,8 @@ from . import views
|
|||||||
|
|
||||||
app_name = "polls"
|
app_name = "polls"
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("", views.index, name="index"),
|
path("", views.IndexView.as_view(), name="index"),
|
||||||
path("<int:question_id>/", views.detail, name="detail"),
|
path("<int:pk>/", views.DetailView.as_view(), name="detail"),
|
||||||
path("<int:question_id>/results/", views.results, name="results"),
|
path("<int:pk>/results/", views.ResultsView.as_view(), name="results"),
|
||||||
path("<int:question_id>/vote/", views.vote, name="vote"),
|
path("<int:question_id>/vote/", views.vote, name="vote"),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
from django.http import HttpResponse
|
from django.http import HttpResponseRedirect
|
||||||
from django.shortcuts import render
|
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.views import generic
|
||||||
|
|
||||||
from .models import Question
|
from .models import Choice, Question
|
||||||
|
|
||||||
|
|
||||||
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 = {"latest_question_list": latest_question_list}
|
# context = {"latest_question_list": latest_question_list}
|
||||||
return render(request, "polls/index.html", context)
|
# return render(request, "polls/index.html", context)
|
||||||
|
|
||||||
|
|
||||||
# def index(request):
|
# def index(request):
|
||||||
@ -28,15 +29,58 @@ def index(request):
|
|||||||
# return render(request, "polls/detail.html", {"question": question})
|
# return render(request, "polls/detail.html", {"question": question})
|
||||||
|
|
||||||
|
|
||||||
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):
|
# def results(request, question_id):
|
||||||
response = "You're looking at the results of question %s."
|
# response = "You're looking at the results of question %s."
|
||||||
return HttpResponse(response % question_id)
|
# return HttpResponse(response % question_id)
|
||||||
|
|
||||||
|
|
||||||
|
# def results(request, question_id):
|
||||||
|
# question = get_object_or_404(Question, pk=question_id)
|
||||||
|
# return render(request, "polls/results.html", {"question": question})
|
||||||
|
|
||||||
|
|
||||||
|
# def vote(request, question_id):
|
||||||
|
# return HttpResponse("You're voting on question %s." % question_id)
|
||||||
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
|
||||||
|
class DetailView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = "polls/detail.html"
|
||||||
|
|
||||||
|
|
||||||
|
class ResultsView(generic.DetailView):
|
||||||
|
model = Question
|
||||||
|
template_name = "polls/results.html"
|
||||||
|
|
||||||
|
|
||||||
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):
|
||||||
|
return render(
|
||||||
|
request,
|
||||||
|
"polls/detail.html",
|
||||||
|
{
|
||||||
|
"question": question,
|
||||||
|
"error_message": "You didn't select a choice.",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
selected_choice.votes += 1
|
||||||
|
selected_choice.save()
|
||||||
|
return HttpResponseRedirect(reverse("polls:results", args=(question.id,)))
|
||||||
Loading…
Reference in New Issue
Block a user