-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Writing your first Django app, part 3
- Loading branch information
Showing
6 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import datetime | ||
|
||
from django.db import models | ||
from django.utils import timezone | ||
|
||
|
||
class Question(models.Model): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<h1>{{ question.question_text }}</h1> | ||
|
||
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %} | ||
|
||
<form action="{% url 'polls:vote' question.id %}" method="post"> | ||
{% csrf_token %} | ||
{% 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 %} | ||
<input type="submit" value="Vote" /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% if latest_question_list %} | ||
<ul> | ||
{% for question in latest_question_list %} | ||
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li> | ||
{% endfor %} | ||
</ul> | ||
{% else %} | ||
<p>No polls are available.</p> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
from django.http import HttpResponse | ||
from django.shortcuts import get_object_or_404, render | ||
from django.http import HttpResponseRedirect | ||
from django.urls import reverse | ||
from django.views import generic | ||
|
||
from .models import Choice, Question | ||
|
||
def index(request): | ||
return HttpResponse("Hello, world. You're at the polls index.") | ||
|
||
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): | ||
question = get_object_or_404(Question, pk=question_id) | ||
try: | ||
selected_choice = question.choice_set.get(pk=request.POST['choice']) | ||
except (KeyError, Choice.DoesNotExist): | ||
# Redisplay the question voting form. | ||
return render(request, 'polls/detail.html', { | ||
'question': question, | ||
'error_message': "You didn't select a choice.", | ||
}) | ||
else: | ||
selected_choice.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,))) |