django: finish tutorial #3

This commit is contained in:
Kyle Isom 2018-05-06 20:21:35 -07:00
parent d930315fb3
commit 25bfca067c
4 changed files with 39 additions and 3 deletions

View File

@ -0,0 +1,6 @@
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

View File

@ -0,0 +1,9 @@
{% if latest_questions %}
<ul>
{% for question in latest_questions %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

View File

@ -2,6 +2,10 @@ 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'),
]

View File

@ -1,6 +1,23 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404, render
from . import models
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
latest_questions = models.Question.objects.order_by('-pub_date')[:5]
ctx = {'latest_questions': latest_questions}
return render(request, 'polls/index.html', ctx)
def detail(request, question_id):
q = get_object_or_404(models.Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': q})
def results(request, question_id):
response = "Tally for \"{}\":".format(question_id)
return HttpResponse(response)
def vote(request, question_id):
return HttpResponse("You're voting on {}".format(question_id))