From 3c1672b7530eb04ce099661541c5c2099c563f93 Mon Sep 17 00:00:00 2001 From: samuel Date: Mon, 13 Jul 2020 17:12:39 +0200 Subject: [PATCH] step4 --- sami/django/mysite2/db.sqlite3 | Bin 0 -> 143360 bytes sami/django/mysite2/mysite2/settings.py | 7 ++- sami/django/mysite2/mysite2/urls.py | 4 +- sami/django/mysite2/polls/.#models.py | 1 + .../mysite2/polls/migrations/0001_initial.py | 32 +++++++++++ sami/django/mysite2/polls/models.py | 24 ++++++++ .../mysite2/polls/templates/polls/detail.html | 1 + .../mysite2/polls/templates/polls/index.html | 22 ++++++++ .../polls/templates/polls/results.html | 9 +++ sami/django/mysite2/polls/urls.py | 13 +++++ sami/django/mysite2/polls/views.py | 52 +++++++++++++++++- 11 files changed, 160 insertions(+), 5 deletions(-) create mode 120000 sami/django/mysite2/polls/.#models.py create mode 100644 sami/django/mysite2/polls/migrations/0001_initial.py create mode 100644 sami/django/mysite2/polls/templates/polls/detail.html create mode 100644 sami/django/mysite2/polls/templates/polls/index.html create mode 100644 sami/django/mysite2/polls/templates/polls/results.html create mode 100644 sami/django/mysite2/polls/urls.py diff --git a/sami/django/mysite2/db.sqlite3 b/sami/django/mysite2/db.sqlite3 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..95016afdcda3372d141ae70285e53f77a5419a45 100644 GIT binary patch literal 143360 zcmeI5du$uYeaCnC5JgMmO0s3%ogboP>uf@2X}QbgLznBF&eqBDSyUv;4=FjYOL8Ty z%!f=;mgQ>-(D5Zj()N!A4bTAX6$Og6X#Z%_0#_itKboLT-zkbDK-#3Qf6@n%YYHSO zkQC_5>@IitkgRikjnDlO_GoA3H^2SPXJ(e1nI-qi(q+A_h1V<9l3EYT+yKY(+)Lpw z$8izz{}lOey)Kdij};+*dB^cyhY@b_#jkm(vG4ML-<7lls?d0{m8)!mTg< zQr~BY5ndnw0w4eaAOHd&00JNY0tX@>4tcrI_{>(NSgeVn7|rNqy{@Z8NtDEMV&Ys> z3`b+ra&%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-^AW81vKXAey3onpAc!2;2fB*=900@8p2!H?xfB*=9!26NFhuwZ|Mi{Q}JTHlo zcuq{5i;CfBd|HlA$CSxbJeHE9Qd!ScKJ@xnuAr7T$itnc!>^NPOlie@jU0Z+@mMGF zbNt4GrC$5UM_%)|T#X*rA?m(yp7*!;my*-b|BlC5krUVtk!qfe95f$T zWga)5qGc#a@~oN@yx-qiO(`)Ql_sN7G?t26GSqzDs3fffo&SgXVw~`G;j_X|30YxU z=nwvV@b`kB4pxKd;AC(p@E?J{3jB89jlfFaRR8z-zuf;@{rCHSy#E9JLw*0!_xZkG z>HCSkYs3LwAOHd&00JNY0w4eaAn=3|2#38Ke`=!lwyy1DjK_POSIc1<4I9zZBgEp& zR4;irrbZqbYugwJmeona%9wXJtc(QH>f{MxH5%z9AKM)!BxZRz&b;`T8tds_O+8k^ z*<-|NLiU-8tYm6uYO<2fK4Vy#s;p#cY3j0)#-1jYT(!SS!Q(N2(dVq@ER>x zDc7}fy}rApHI<(nFrMbNM?wOJOXC>nhxKrBv-d#D&I zo2sD4C;Lg>XvEFVzEQ1gZ#7TPVq<+IfBc-=Jb%-|6kymKGHgr*O&e1Jiy!wBn~^ha zs-bB>MX<=(UXnW@yO;|5sk}`ocG7b`VmXy?nR@IDYHNzJ6QAobtW9Nh2DLWj*@@43 ziFG9Eq8jZ~YD$ILDW^SN&VMG##FFP?lb%-HZAv8xVU}{%O{^vqQ>fF= timezone.now() -datetime.timedelta(days=1) + +class Choice(models.Model): + question = models.ForeignKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) + + def __str__(self): + return self.choice_text + + diff --git a/sami/django/mysite2/polls/templates/polls/detail.html b/sami/django/mysite2/polls/templates/polls/detail.html new file mode 100644 index 0000000..6667640 --- /dev/null +++ b/sami/django/mysite2/polls/templates/polls/detail.html @@ -0,0 +1 @@ +{{ question }} diff --git a/sami/django/mysite2/polls/templates/polls/index.html b/sami/django/mysite2/polls/templates/polls/index.html new file mode 100644 index 0000000..71c2249 --- /dev/null +++ b/sami/django/mysite2/polls/templates/polls/index.html @@ -0,0 +1,22 @@ +{% if latest_question_list %} + +{% else %} +

No polls are available.

+{% endif %} + +

{{ question.question_text }}

+ +{% if error_message %}

{{ error_message }}

{% endif %} + +
+{% csrf_token %} +{% for choice in question.choice_set.all %} + +
+{% endfor %} + +
diff --git a/sami/django/mysite2/polls/templates/polls/results.html b/sami/django/mysite2/polls/templates/polls/results.html new file mode 100644 index 0000000..3b2c74f --- /dev/null +++ b/sami/django/mysite2/polls/templates/polls/results.html @@ -0,0 +1,9 @@ +

{{ question.question_text }}

+ +
    +{% for choice in question.choice_set.all %} +
  • {{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}
  • +{% endfor %} +
+ +Vote again? diff --git a/sami/django/mysite2/polls/urls.py b/sami/django/mysite2/polls/urls.py new file mode 100644 index 0000000..0b66ce8 --- /dev/null +++ b/sami/django/mysite2/polls/urls.py @@ -0,0 +1,13 @@ +from django.urls import path +# Here is importing the views which I modified +from . import views +app_name = 'polls' +urlpatterns = [ + 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/sami/django/mysite2/polls/views.py b/sami/django/mysite2/polls/views.py index 91ea44a..b018678 100644 --- a/sami/django/mysite2/polls/views.py +++ b/sami/django/mysite2/polls/views.py @@ -1,3 +1,53 @@ +# Here is the fist view add by Sami from Diesbach from django.shortcuts import render +from django.http import HttpResponse +from django.shortcuts import Http404 +from .models import Choice, Question +from django.template import loader +from django.shortcuts import get_object_or_404, render +from django.http import HttpResponse, HttpResponseRedirect +from django.urls import reverse +from django.views import generic + + + +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] + + +class DetailView(generic.DetailView): + model = Question + template_name = 'polls/detail.html' + + +class ResultsView(generic.DetailView): + model = Question + template_name = 'polls/results.html' + + + + +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']) + 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,))) + -# Create your views here.