From f270905d018499b67c57f17abedc9b19d344e228 Mon Sep 17 00:00:00 2001 From: kjg Date: Fri, 10 Jul 2020 22:41:45 +0900 Subject: [PATCH] [Django #5] Tutorial 4 --- kjg/django/mysite/mysite/settings.py | 4 +- kjg/django/mysite/mysite/urls.py | 5 +- kjg/django/mysite/polls/admin.py | 3 +- .../mysite/polls/migrations/0001_initial.py | 32 ++++++++++ .../mysite/polls/templates/polls/detail.html | 12 ++++ .../mysite/polls/templates/polls/index.html | 9 +++ .../mysite/polls/templates/polls/results.html | 9 +++ kjg/django/mysite/polls/urls.py | 6 +- kjg/django/mysite/polls/views.py | 60 ++++++++++++++++++- 9 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 kjg/django/mysite/polls/migrations/0001_initial.py create mode 100644 kjg/django/mysite/polls/templates/polls/detail.html create mode 100644 kjg/django/mysite/polls/templates/polls/index.html create mode 100644 kjg/django/mysite/polls/templates/polls/results.html diff --git a/kjg/django/mysite/mysite/settings.py b/kjg/django/mysite/mysite/settings.py index ee85ab2..ec0bcd2 100644 --- a/kjg/django/mysite/mysite/settings.py +++ b/kjg/django/mysite/mysite/settings.py @@ -25,8 +25,8 @@ SECRET_KEY = '2kbmvs@pcbxn)kt#p9$)45=h#i8_b7^^l&l57mbe4%e=+k91-d' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [ "[2a0a:e5c1:151::12]" ] - +ALLOWED_HOSTS = [ "[2a0a:e5c1:114::12]", "127.0.0.1", "[2a0a:e5c1:151::12]", "testjg.django.lab.ungleich.ch", "[2a0a:e5c1:114::17]" ] +#ALLOWED_HOSTS = [] # Application definition diff --git a/kjg/django/mysite/mysite/urls.py b/kjg/django/mysite/mysite/urls.py index 8a95eac..2f884d2 100644 --- a/kjg/django/mysite/mysite/urls.py +++ b/kjg/django/mysite/mysite/urls.py @@ -14,8 +14,11 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import include, path + urlpatterns = [ + path('polls/', include('polls.urls')), path('admin/', admin.site.urls), ] + diff --git a/kjg/django/mysite/polls/admin.py b/kjg/django/mysite/polls/admin.py index 6af8ff6..ffd3760 100644 --- a/kjg/django/mysite/polls/admin.py +++ b/kjg/django/mysite/polls/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin -from .models import Question +from .models import Question, Choice admin.site.register(Question) +admin.site.register(Choice) diff --git a/kjg/django/mysite/polls/migrations/0001_initial.py b/kjg/django/mysite/polls/migrations/0001_initial.py new file mode 100644 index 0000000..f358828 --- /dev/null +++ b/kjg/django/mysite/polls/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 3.0.8 on 2020-07-06 15:51 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Question', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('question_text', models.CharField(max_length=200)), + ('pub_date', models.DateTimeField(verbose_name='date published')), + ], + ), + migrations.CreateModel( + name='Choice', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('choice_text', models.CharField(max_length=200)), + ('votes', models.IntegerField(default=0)), + ('question', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question')), + ], + ), + ] diff --git a/kjg/django/mysite/polls/templates/polls/detail.html b/kjg/django/mysite/polls/templates/polls/detail.html new file mode 100644 index 0000000..3e55544 --- /dev/null +++ b/kjg/django/mysite/polls/templates/polls/detail.html @@ -0,0 +1,12 @@ +

{{ question.question_text }}

+ +{% if error_message %}

{{ error_message }}

{% endif %} + +
+{% csrf_token %} +{% for choice in question.choice_set.all %} + +
+{% endfor %} + +
diff --git a/kjg/django/mysite/polls/templates/polls/index.html b/kjg/django/mysite/polls/templates/polls/index.html new file mode 100644 index 0000000..4560139 --- /dev/null +++ b/kjg/django/mysite/polls/templates/polls/index.html @@ -0,0 +1,9 @@ +{% if latest_question_list %} + +{% else %} +

No polls are available.

+{% endif %} diff --git a/kjg/django/mysite/polls/templates/polls/results.html b/kjg/django/mysite/polls/templates/polls/results.html new file mode 100644 index 0000000..3b2c74f --- /dev/null +++ b/kjg/django/mysite/polls/templates/polls/results.html @@ -0,0 +1,9 @@ +

{{ question.question_text }}

+ + + +Vote again? diff --git a/kjg/django/mysite/polls/urls.py b/kjg/django/mysite/polls/urls.py index 88a9cac..eff2be0 100644 --- a/kjg/django/mysite/polls/urls.py +++ b/kjg/django/mysite/polls/urls.py @@ -2,6 +2,10 @@ from django.urls import path from . import views +app_name = 'polls' urlpatterns = [ - path('', views.index, name='index'), + path('', views.IndexView.as_view(), name='index'), + path('/', views.DetailView.as_view(), name='detail'), + path('/results/', views.ResultsView.as_view(), name='results'), + path('/vote/', views.vote, name='vote'), ] diff --git a/kjg/django/mysite/polls/views.py b/kjg/django/mysite/polls/views.py index 91ea44a..584507f 100644 --- a/kjg/django/mysite/polls/views.py +++ b/kjg/django/mysite/polls/views.py @@ -1,3 +1,59 @@ -from django.shortcuts import render +from django.http import HttpResponse, HttpResponseRedirect +#from django.http import Http404 +from django.template import loader +from django.shortcuts import get_object_or_404, render +from django.urls import reverse +from django.views import generic -# Create your views here. +from .models import Choice, 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) +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] + +#def detail(request, question_id): +# #return HttpResponse("You're looking at question %s." % question_id) +# question = get_object_or_404(Question, pk=question_id) +# return render(request, 'polls/detail.html', {'question': question}) +class DetailView(generic.DetailView): + model = Question + template_name = 'polls/detail.html' + + +#def results(request, question_id): +# #response = "You're looking at the results of question %s." +# #return HttpResponse(response % question_id) +# question = get_object_or_404(Question, pk=question_id) +# return render(request, 'polls/results.html', {'question': question}) +class ResultsView(generic.DetailView): + model = Question + template_name = 'polls/results.html' + + +def vote(request, question_id): + #return HttpResponse("You're voting on question %s." % 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,)))