django tutorial #3 done

This commit is contained in:
pedrolab 2020-07-13 13:55:28 +02:00
parent 1fc6af7553
commit 8b9bf5030f
5 changed files with 90 additions and 4 deletions

View file

@ -25,7 +25,12 @@ SECRET_KEY = '37jgpv28n1x!f3-48h)4ad99&jz-ek$@e6uf*an5uve6%7atqq'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
ALLOWED_HOSTS = [
"localhost",
"2a0a-e5c1-011f-0000-0000-0000-0000-0001.has-a.name"
]
# Application definition

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,13 @@
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
{# <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li> #}
{# refactor the hardcoded URL #}
{# <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li> #}
{# use URL namespace #}
<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,15 @@ from django.urls import path
from . import views
# namespacing URLs
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
# 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>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]

View file

@ -1,9 +1,62 @@
from django.shortcuts import render
from django.shortcuts import get_object_or_404, render
# Create your views here.
from django.http import HttpResponse
from django.template import loader
from .models import Question
from django.http import Http404
## simple index
#def index(request):
# return HttpResponse("Hello, world. You're at the polls index.")
## meaningful index
#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)
## meaningful and templated index
#def index(request):
# latest_question_list = Question.objects.order_by('-pub_date')[:5]
# template = loader.get_template('polls/index.html')
# context = {
# 'latest_question_list': latest_question_list,
# }
# return HttpResponse(template.render(context, request))
## meaningful, templated and easy to write index
## and then we no longer need to import loader and HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {
'latest_question_list': latest_question_list,
}
return render(request, 'polls/index.html', context)
## simple detail
#def detail(request, question_id):
# return HttpResponse("You're looking at question %s." % question_id)
## meaningful detail
#def detail(request, question_id):
# try:
# question = Question.objects.get(pk=question_id)
# except Question.DoesNotExist:
# raise Http404("Question does not exist")
# return render(request, 'polls/detail.html', {'question': question})
## meaningful and easy to write detail
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question': question})
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)