unknown 3 bulan lalu
induk
melakukan
0c46e90d4b
5 mengubah file dengan 63 tambahan dan 26 penghapusan
  1. TEMPAT SAMPAH
      db.sqlite3
  2. 12 6
      polls/templates/polls/detail.html
  3. 9 0
      polls/templates/polls/results.html
  4. 3 7
      polls/urls.py
  5. 39 13
      polls/views.py

TEMPAT SAMPAH
db.sqlite3


+ 12 - 6
polls/templates/polls/detail.html

@@ -1,6 +1,12 @@
-<h1>{{ question.question_text }}</h1>
-<ul>
-{% for choice in question.choice_set.all %}
-    <li>{{ chouice.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_message }}</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>

+ 9 - 0
polls/templates/polls/results.html

@@ -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>

+ 3 - 7
polls/urls.py

@@ -3,12 +3,8 @@ from . import views
 
 
 app_name = "polls"
 app_name = "polls"
 urlpatterns = [
 urlpatterns = [
-    # ex: /polls/
-    path("", views.index, name='index'),
-    # ex: /polls/5/
-    path("<int:question_id>/", views.detail, name="detail"),
-    # ex: /polls/5/results/
-    path("<int:question_id>/result/", views.results, name="results"),
-    # ex: /polls/5/vote/
+    path("", views.IndexView.as_view(), name="index"),
+    path("<int:pk>/", views.DetailView.as_view(), name="detail"),
+    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"),
 ]
 ]

+ 39 - 13
polls/views.py

@@ -1,24 +1,50 @@
-from django.http import HttpResponse
+from django.db.models import F
+from django.http import HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render
 from django.shortcuts import get_object_or_404, render
-from .models import Question
+from django.urls import reverse
+from django.views import generic
+
+from .models import Choice, Question
 
 
 
 
 # Create your views here.
 # Create your views here.
 
 
-def index(request):
-    latest_question_list = Question.objects.order_by("-pub_date")[:5]
-    context = {"latest_question_list": latest_question_list}
-    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 poblished questions."""
+        return Question.objects.order_by("-pub_date")[:5]
 
 
-def detail(request, question_id):
-    question = get_object_or_404(Question, pk=question_id)
-    return render(request, "polls/detail.html", {"question": question})
 
 
+class DetailView(generic.DetailView):
+    model = Question
+    template_name = "polls/detail.html"
 
 
-def results(request, question_id):
-    responce = "You're looking at the results of question %s."
-    return HttpResponse(responce % question_id)
+
+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 = F("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,)))