This file contains hidden or 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
from django.shortcuts import get_object_or_404, render | |
from django.http import HttpResponseRedirect, HttpResponse | |
from django.urls import reverse | |
from .models import Choice, Question | |
def results(request, question_id): | |
question = get_object_or_404(Question, pk=question_id) | |
return render(request, 'polls/results.html', {'question': question}) |
This file contains hidden or 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
from django.shortcuts import get_object_or_404, render | |
from django.http import HttpResponseRedirect, HttpResponse | |
from django.urls import reverse | |
from .models import Choice, Question | |
# ... | |
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']) |
This file contains hidden or 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
from django.shortcuts import get_object_or_404, render | |
from django.http import HttpResponseRedirect, HttpResponse | |
from django.urls import reverse | |
from .models import Choice, Question | |
# ... | |
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']) |
This file contains hidden or 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
<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 %} |
This file contains hidden or 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
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'), | |
path('<int:question_id>/results/', views.results, name='results'), | |
path('<int:question_id>/vote/', views.vote, name='vote'), |
This file contains hidden or 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
from django.http import Http404 | |
from django.shortcuts import render | |
from .models import Question | |
# ... | |
def detail(request, question_id): | |
try: | |
question = Question.objects.get(pk=question_id) | |
except Question.DoesNotExist: | |
raise Http404("Question does not exist") |
This file contains hidden or 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
from django.shortcuts import render | |
from .models import Question | |
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) |
This file contains hidden or 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
from django.http import HttpResponse | |
from django.template import loader | |
from .models import Question | |
def index(request): | |
latest_question_list = Question.objects.order_by('-pub_date')[:5] | |
template = loader.get_template('polls/index.html') | |
context = { |
This file contains hidden or 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
{% if latest_question_list %} | |
<ul> | |
{% for question in latest_question_list %} | |
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li> | |
{% endfor %} | |
</ul> | |
{% else %} | |
<p>No polls are available.</p> | |
{% endif %} |
This file contains hidden or 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
from django.http import HttpResponse | |
from .models import Question | |
def index(request): | |
latest_question_list = Question.objects.order_by('-pub_date')[:5] | |
output = ', '.join([q.question_text for q in latest_question_list]) | |
return HttpResponse(output) |