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

main
roger 3 years ago
parent 6792a0795c
commit 7e7dc38c6f
  1. 19
      polls/templates/detail.html
  2. 4
      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> </head>
<body> <body>
<h1>{{ question.question_text }}</h1> <h1>{{ question.question_text }}</h1>
<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 %} {% 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>
</body> </body>
</html> </html>

@ -8,7 +8,9 @@
{% if latest_question_list %} {% if latest_question_list %}
<ul> <ul>
{% for question in latest_question_list %} {% 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 %} {% endfor %}
</ul> </ul>
{% else %} {% else %}

@ -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 from . import views
app_name = 'polls'
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'), path('<int:question_id>/', views.detail, name='detail'),

@ -1,9 +1,10 @@
from django.shortcuts import render from django.shortcuts import render
from django.shortcuts import get_object_or_404 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 django.http import Http404
from .models import Question from .models import Choice, Question
from django.template import loader from django.template import loader
from django.urls import reverse
# Create your views here. # Create your views here.
@ -40,9 +41,21 @@ 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, 'detail.html', {'question': question}) return render(request, 'detail.html', {'question': question})
def result(request, question_id): 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): 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