adds a view for voting on a question
This commit is contained in:
parent
da055416ee
commit
d9eb2540d9
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
|
@ -19,4 +19,4 @@ class Choice(models.Model):
|
||||||
votes = models.IntegerField(default=0)
|
votes = models.IntegerField(default=0)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.choice_text
|
return self.choice_text
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
<h3>Question:</h3>
|
<form action="{% url 'polls:vote' question.id %}" method="post">
|
||||||
<p>{{ question.question_text }}</p>
|
{% csrf_token %}
|
||||||
|
<fieldset>
|
||||||
<h3>Available Choices:</h3>
|
<legend><h1>{{ question.question_text }}</h1></legend>
|
||||||
<ul>
|
{% 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>
|
|
@ -2,6 +2,8 @@ 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,6 +1,7 @@
|
||||||
from django.http import HttpResponse, Http404
|
from django.http import HttpResponse, Http404, HttpResponseRedirect
|
||||||
from django.template import loader
|
from django.template import loader
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render, get_object_or_404
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
from .models import Question, Choice
|
from .models import Question, Choice
|
||||||
|
|
||||||
|
@ -24,4 +25,16 @@ def results(request, question_id):
|
||||||
return HttpResponse(response % question_id)
|
return HttpResponse(response % question_id)
|
||||||
|
|
||||||
def vote(request, question_id):
|
def vote(request, question_id):
|
||||||
return HttpResponse("you are 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/details.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