add detail view and result view, vote function can be used.

main
roger 2 years ago
parent 6792a0795c
commit 7e7dc38c6f
  1. 23
      polls/templates/detail.html
  2. 16
      polls/templates/index.html
  3. 19
      polls/templates/results.html
  4. 1
      polls/urls.py
  5. 21
      polls/views.py

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

@ -5,14 +5,16 @@
<title>Title</title>
</head>
<body>
{% if latest_question_list %}
<ul>
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="https://127.0.0.1/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{# <li><a href="http://127.0.0.1:8000/polls/{{ question.id }}/">{{ question.question_text }}</a></li>#}
<li><a href="http://127.0.0.1:8000{% url 'polls:detail' question.id %}">{{ question.question_text }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<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="http://127.0.0.1:8000{% url 'polls:detail' question.id %}">Vote again?</a>
</body>
</html>

@ -2,6 +2,7 @@ from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),

@ -1,9 +1,10 @@
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.http import Http404
from .models import Question
from .models import Choice, Question
from django.template import loader
from django.urls import reverse
# Create your views here.
@ -40,9 +41,21 @@ def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'detail.html', {'question': question})
def result(request, question_id):
return HttpResponse(f"You're looking at the result of question {question_id}.")
question = get_object_or_404(Question, pk=question_id)
return render(request, 'results.html', {'question': question})
# return HttpResponse(f"You're looking at the result of question {question_id}.")
def vote(request, question_id):
return HttpResponse(f"You're voting on question {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, 'detail.html', {'question': question, 'error_message': "You didn't select a choice.", })
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:result', args=(question_id,)))
# return HttpResponse(f"You're voting on question {question_id}")

Loading…
Cancel
Save