From 93186e25011cdaeb652a8abfd4f1f037803b7d71 Mon Sep 17 00:00:00 2001 From: Lukas Date: Fri, 27 Aug 2021 16:50:01 +0200 Subject: [PATCH] Part 2 Tutorial Done --- djlearn/djlearn/settings.py | 3 ++- djlearn/djlearn/urls.py | 3 ++- djlearn/polls/__init__.py | 0 djlearn/polls/admin.py | 6 +++++ djlearn/polls/apps.py | 6 +++++ djlearn/polls/migrations/0001_initial.py | 32 ++++++++++++++++++++++++ djlearn/polls/migrations/__init__.py | 0 djlearn/polls/models.py | 28 +++++++++++++++++++++ djlearn/polls/tests.py | 3 +++ djlearn/polls/urls.py | 6 +++++ djlearn/polls/views.py | 8 ++++++ 11 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 djlearn/polls/__init__.py create mode 100644 djlearn/polls/admin.py create mode 100644 djlearn/polls/apps.py create mode 100644 djlearn/polls/migrations/0001_initial.py create mode 100644 djlearn/polls/migrations/__init__.py create mode 100644 djlearn/polls/models.py create mode 100644 djlearn/polls/tests.py create mode 100644 djlearn/polls/urls.py create mode 100644 djlearn/polls/views.py diff --git a/djlearn/djlearn/settings.py b/djlearn/djlearn/settings.py index 3562ccf..ccc53c2 100644 --- a/djlearn/djlearn/settings.py +++ b/djlearn/djlearn/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'polls.apps.PollsConfig', ] MIDDLEWARE = [ @@ -105,7 +106,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'Europe/Berlin' USE_I18N = True diff --git a/djlearn/djlearn/urls.py b/djlearn/djlearn/urls.py index 77fff82..cd0e153 100644 --- a/djlearn/djlearn/urls.py +++ b/djlearn/djlearn/urls.py @@ -14,8 +14,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/djlearn/polls/__init__.py b/djlearn/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djlearn/polls/admin.py b/djlearn/polls/admin.py new file mode 100644 index 0000000..9e7a581 --- /dev/null +++ b/djlearn/polls/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import Question + +# Register your models here. + +admin.site.register(Question) diff --git a/djlearn/polls/apps.py b/djlearn/polls/apps.py new file mode 100644 index 0000000..5a5f94c --- /dev/null +++ b/djlearn/polls/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class PollsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'polls' diff --git a/djlearn/polls/migrations/0001_initial.py b/djlearn/polls/migrations/0001_initial.py new file mode 100644 index 0000000..48be4bb --- /dev/null +++ b/djlearn/polls/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 3.2.6 on 2021-08-27 14:31 + +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.BigAutoField(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.BigAutoField(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/djlearn/polls/migrations/__init__.py b/djlearn/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/djlearn/polls/models.py b/djlearn/polls/models.py new file mode 100644 index 0000000..b2023a6 --- /dev/null +++ b/djlearn/polls/models.py @@ -0,0 +1,28 @@ +import datetime + +from django.db import models +from django.utils import timezone + +# Create your models here. + + +class Question(models.Model): + + def __str__(self): + return self.question_text + + def was_published_recently(self): + return self.pub_date >= timezone.now() - datetime.timedelta(days=1) + + question_text = models.CharField(max_length=200) + pub_date = models.DateTimeField('date published') + + +class Choice(models.Model): + + def __str__(self): + return self.choice_text + + question = models.ForeignKey(Question, on_delete=models.CASCADE) + choice_text = models.CharField(max_length=200) + votes = models.IntegerField(default=0) diff --git a/djlearn/polls/tests.py b/djlearn/polls/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/djlearn/polls/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/djlearn/polls/urls.py b/djlearn/polls/urls.py new file mode 100644 index 0000000..9a62f74 --- /dev/null +++ b/djlearn/polls/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path("", views.index, name="index") +] diff --git a/djlearn/polls/views.py b/djlearn/polls/views.py new file mode 100644 index 0000000..18923c8 --- /dev/null +++ b/djlearn/polls/views.py @@ -0,0 +1,8 @@ +from django.shortcuts import render +from django.http import HttpResponse + +# Create your views here. + + +def index(request): + return HttpResponse("Hello World. This is the Polls index")