diff --git a/sami/django/mysite2/db.sqlite3 b/sami/django/mysite2/db.sqlite3 index e69de29..95016af 100644 Binary files a/sami/django/mysite2/db.sqlite3 and b/sami/django/mysite2/db.sqlite3 differ diff --git a/sami/django/mysite2/mysite2/settings.py b/sami/django/mysite2/mysite2/settings.py index 9db30f1..a23a153 100644 --- a/sami/django/mysite2/mysite2/settings.py +++ b/sami/django/mysite2/mysite2/settings.py @@ -25,12 +25,13 @@ SECRET_KEY = 'sb1cbc!m_esjnb381eh8!+(1$zi-%$h@ewu#3=cog+ls)d%)3z' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] - +ALLOWED_HOSTS = ['2a0a-e5c0-10-bee-7994-d023-5c29-7091.has-a.name'] + # Application definition INSTALLED_APPS = [ + 'polls.apps.PollsConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', @@ -105,7 +106,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'Europe/Zurich' USE_I18N = True diff --git a/sami/django/mysite2/mysite2/urls.py b/sami/django/mysite2/mysite2/urls.py index 7ab8fd1..e7a42d7 100644 --- a/sami/django/mysite2/mysite2/urls.py +++ b/sami/django/mysite2/mysite2/urls.py @@ -1,3 +1,4 @@ + """mysite2 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: @@ -14,8 +15,9 @@ 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/sami/django/mysite2/polls/.#models.py b/sami/django/mysite2/polls/.#models.py new file mode 120000 index 0000000..3884ade --- /dev/null +++ b/sami/django/mysite2/polls/.#models.py @@ -0,0 +1 @@ +sami@afro-linux-lenovo-b50-30.8898:1594292241 \ No newline at end of file diff --git a/sami/django/mysite2/polls/migrations/0001_initial.py b/sami/django/mysite2/polls/migrations/0001_initial.py new file mode 100644 index 0000000..3826812 --- /dev/null +++ b/sami/django/mysite2/polls/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 2.2.12 on 2020-07-10 14:30 + +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/sami/django/mysite2/polls/models.py b/sami/django/mysite2/polls/models.py index 71a8362..1914ee2 100644 --- a/sami/django/mysite2/polls/models.py +++ b/sami/django/mysite2/polls/models.py @@ -1,3 +1,27 @@ +from django.utils import timezone from django.db import models +import datetime + # Create your models here. + + +class Question(models.Model): + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + def __str__(self): + return self.question_text + + def was_published_recently(self): + return self.pub_date >= 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 }}

+ + + +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.