From 8b9bf5030f2816a016ebe7cf02a17e1b74f6b389 Mon Sep 17 00:00:00 2001 From: pedrolab <531310-pedrolab@users.noreply.gitlab.com> Date: Mon, 13 Jul 2020 13:55:28 +0200 Subject: [PATCH] django tutorial #3 done --- .../django-tutorial/mysite/mysite/settings.py | 7 ++- .../mysite/polls/templates/polls/detail.html | 6 ++ .../mysite/polls/templates/polls/index.html | 13 +++++ pedro/django-tutorial/mysite/polls/urls.py | 11 +++- pedro/django-tutorial/mysite/polls/views.py | 57 ++++++++++++++++++- 5 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 pedro/django-tutorial/mysite/polls/templates/polls/detail.html create mode 100644 pedro/django-tutorial/mysite/polls/templates/polls/index.html diff --git a/pedro/django-tutorial/mysite/mysite/settings.py b/pedro/django-tutorial/mysite/mysite/settings.py index 82ed813..0e2091c 100644 --- a/pedro/django-tutorial/mysite/mysite/settings.py +++ b/pedro/django-tutorial/mysite/mysite/settings.py @@ -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 diff --git a/pedro/django-tutorial/mysite/polls/templates/polls/detail.html b/pedro/django-tutorial/mysite/polls/templates/polls/detail.html new file mode 100644 index 0000000..975db2a --- /dev/null +++ b/pedro/django-tutorial/mysite/polls/templates/polls/detail.html @@ -0,0 +1,6 @@ +

{{ question.question_text }}

+ diff --git a/pedro/django-tutorial/mysite/polls/templates/polls/index.html b/pedro/django-tutorial/mysite/polls/templates/polls/index.html new file mode 100644 index 0000000..b1b7ea8 --- /dev/null +++ b/pedro/django-tutorial/mysite/polls/templates/polls/index.html @@ -0,0 +1,13 @@ +{% if latest_question_list %} + +{% else %} +

No polls are available.

+{% endif %} diff --git a/pedro/django-tutorial/mysite/polls/urls.py b/pedro/django-tutorial/mysite/polls/urls.py index 88a9cac..3e302ea 100644 --- a/pedro/django-tutorial/mysite/polls/urls.py +++ b/pedro/django-tutorial/mysite/polls/urls.py @@ -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('/', views.detail, name='detail'), + # ex: /polls/5/results/ + path('/results/', views.results, name='results'), + # ex: /polls/5/vote/ + path('/vote/', views.vote, name='vote'), ] diff --git a/pedro/django-tutorial/mysite/polls/views.py b/pedro/django-tutorial/mysite/polls/views.py index 6dcc115..257fe14 100644 --- a/pedro/django-tutorial/mysite/polls/views.py +++ b/pedro/django-tutorial/mysite/polls/views.py @@ -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)