commit ac1f869c94558d3095fb4104afb0d6071955145e Author: Anish Lakhwara Date: Thu Jul 2 12:31:58 2020 +1000 initial commit, up to part 4/5 diff --git a/mysite/db.sqlite3 b/mysite/db.sqlite3 new file mode 100644 index 0000000..76f8333 Binary files /dev/null and b/mysite/db.sqlite3 differ diff --git a/mysite/manage.py b/mysite/manage.py new file mode 100755 index 0000000..afbc784 --- /dev/null +++ b/mysite/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + try: + from django.core.management import execute_from_command_line + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) + raise + execute_from_command_line(sys.argv) diff --git a/mysite/mysite/__init__.py b/mysite/mysite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/mysite/__pycache__/__init__.cpython-37.pyc b/mysite/mysite/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..caa798b Binary files /dev/null and b/mysite/mysite/__pycache__/__init__.cpython-37.pyc differ diff --git a/mysite/mysite/__pycache__/settings.cpython-37.pyc b/mysite/mysite/__pycache__/settings.cpython-37.pyc new file mode 100644 index 0000000..fe3c765 Binary files /dev/null and b/mysite/mysite/__pycache__/settings.cpython-37.pyc differ diff --git a/mysite/mysite/__pycache__/urls.cpython-37.pyc b/mysite/mysite/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000..e10d518 Binary files /dev/null and b/mysite/mysite/__pycache__/urls.cpython-37.pyc differ diff --git a/mysite/mysite/__pycache__/wsgi.cpython-37.pyc b/mysite/mysite/__pycache__/wsgi.cpython-37.pyc new file mode 100644 index 0000000..9840dba Binary files /dev/null and b/mysite/mysite/__pycache__/wsgi.cpython-37.pyc differ diff --git a/mysite/mysite/settings.py b/mysite/mysite/settings.py new file mode 100644 index 0000000..af24bbf --- /dev/null +++ b/mysite/mysite/settings.py @@ -0,0 +1,121 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 1.11.29. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/ref/settings/ +""" + +import os + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'm2h0wcn#80^c9*(+62i--zwft1c_4h0+8q#yte&9vu7s6l67!(' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'polls.apps.PollsConfig', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'mysite.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'mysite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/1.11/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.11/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/mysite/mysite/urls.py b/mysite/mysite/urls.py new file mode 100644 index 0000000..eef98d3 --- /dev/null +++ b/mysite/mysite/urls.py @@ -0,0 +1,22 @@ +"""mysite URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/1.11/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.conf.urls import url, include + 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) +""" +from django.conf.urls import url, include +from django.contrib import admin + +urlpatterns = [ + url(r'^polls/', include('polls.urls')), + url(r'^admin/', admin.site.urls), +] diff --git a/mysite/mysite/wsgi.py b/mysite/mysite/wsgi.py new file mode 100644 index 0000000..74e7dae --- /dev/null +++ b/mysite/mysite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for mysite project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings") + +application = get_wsgi_application() diff --git a/mysite/polls/__init__.py b/mysite/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/polls/__pycache__/__init__.cpython-37.pyc b/mysite/polls/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..7e11076 Binary files /dev/null and b/mysite/polls/__pycache__/__init__.cpython-37.pyc differ diff --git a/mysite/polls/__pycache__/admin.cpython-37.pyc b/mysite/polls/__pycache__/admin.cpython-37.pyc new file mode 100644 index 0000000..c7dbf5f Binary files /dev/null and b/mysite/polls/__pycache__/admin.cpython-37.pyc differ diff --git a/mysite/polls/__pycache__/apps.cpython-37.pyc b/mysite/polls/__pycache__/apps.cpython-37.pyc new file mode 100644 index 0000000..429cbd0 Binary files /dev/null and b/mysite/polls/__pycache__/apps.cpython-37.pyc differ diff --git a/mysite/polls/__pycache__/models.cpython-37.pyc b/mysite/polls/__pycache__/models.cpython-37.pyc new file mode 100644 index 0000000..c00f228 Binary files /dev/null and b/mysite/polls/__pycache__/models.cpython-37.pyc differ diff --git a/mysite/polls/__pycache__/urls.cpython-37.pyc b/mysite/polls/__pycache__/urls.cpython-37.pyc new file mode 100644 index 0000000..10b912c Binary files /dev/null and b/mysite/polls/__pycache__/urls.cpython-37.pyc differ diff --git a/mysite/polls/__pycache__/views.cpython-37.pyc b/mysite/polls/__pycache__/views.cpython-37.pyc new file mode 100644 index 0000000..ff3ed69 Binary files /dev/null and b/mysite/polls/__pycache__/views.cpython-37.pyc differ diff --git a/mysite/polls/admin.py b/mysite/polls/admin.py new file mode 100644 index 0000000..5832a6d --- /dev/null +++ b/mysite/polls/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import Question +# Register your models here. +admin.site.register(Question) diff --git a/mysite/polls/apps.py b/mysite/polls/apps.py new file mode 100644 index 0000000..d0f109e --- /dev/null +++ b/mysite/polls/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class PollsConfig(AppConfig): + name = 'polls' diff --git a/mysite/polls/migrations/0001_initial.py b/mysite/polls/migrations/0001_initial.py new file mode 100644 index 0000000..f28d63f --- /dev/null +++ b/mysite/polls/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 3.0.7 on 2020-06-22 14:12 + +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/mysite/polls/migrations/__init__.py b/mysite/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/polls/migrations/__pycache__/0001_initial.cpython-37.pyc b/mysite/polls/migrations/__pycache__/0001_initial.cpython-37.pyc new file mode 100644 index 0000000..d37e6f6 Binary files /dev/null and b/mysite/polls/migrations/__pycache__/0001_initial.cpython-37.pyc differ diff --git a/mysite/polls/migrations/__pycache__/__init__.cpython-37.pyc b/mysite/polls/migrations/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..8005e44 Binary files /dev/null and b/mysite/polls/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/mysite/polls/models.py b/mysite/polls/models.py new file mode 100644 index 0000000..598052f --- /dev/null +++ b/mysite/polls/models.py @@ -0,0 +1,24 @@ +from django.db import models +import datetime +from django.utils import timezone + +# 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 wasRecent(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/mysite/polls/templates/polls/detail.html b/mysite/polls/templates/polls/detail.html new file mode 100644 index 0000000..0c4fa5b --- /dev/null +++ b/mysite/polls/templates/polls/detail.html @@ -0,0 +1,13 @@ +

{{ question }}

+ +{% if error_message %}

{{ error_message }}

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

No Polls available.

+{% endif %} diff --git a/mysite/polls/templates/polls/result.html b/mysite/polls/templates/polls/result.html new file mode 100644 index 0000000..74cf879 --- /dev/null +++ b/mysite/polls/templates/polls/result.html @@ -0,0 +1,9 @@ +

{{ question }}

+ + + +Vote again? diff --git a/mysite/polls/tests.py b/mysite/polls/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/mysite/polls/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/mysite/polls/urls.py b/mysite/polls/urls.py new file mode 100644 index 0000000..06524e9 --- /dev/null +++ b/mysite/polls/urls.py @@ -0,0 +1,11 @@ +from django.urls import path + +from . import views + +app_name = 'polls' +urlpatterns = [ + path('', views.index, name='index'), + path('/', views.detail, name='detail'), + path('/results/', views.result, name='result'), + path('/vote/', views.vote, name='vote') +] diff --git a/mysite/polls/views.py b/mysite/polls/views.py new file mode 100644 index 0000000..9905f27 --- /dev/null +++ b/mysite/polls/views.py @@ -0,0 +1,31 @@ +from django.shortcuts import render, get_object_or_404 +from django.http import HttpResponse, Http404, HttpResponseRedirect +from django.template import loader +from django.urls import reverse +from .models import Question + +# Create your views here. +def index(request): + question_list = Question.objects.order_by('-pub_date')[:5] + context = { 'question_list': question_list } + return render(request, 'polls/index.html', context) + +def detail(request, question_id): + question = get_object_or_404(Question, pk=question_id) + return render(request, 'polls/detail.html', {'question': question}) + +def result(request, question_id): + question = get_object_or_404(Question, pk=question_id) + return render(request, 'polls/result.html', {'question': question}) + +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): + return render(request, 'polls/detail.html', {"question": question, "error_message": "You didn't select a choice",}) + else: + selected_choice.votes += 1 + selected_choice.save() + return HttpResponseRedirect(reverse('polls:result', args=(question.id, ))) +