From 1224283deec6d458d7a8c22754b15d6c66bbbd3e Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Thu, 15 Oct 2020 15:32:20 +1100 Subject: [PATCH] Worked up to Chapter 3.5, added 3.5 & 3.6 --- src/chap3/chap3_5/lists/__init__.py | 0 src/chap3/chap3_5/lists/admin.py | 3 + src/chap3/chap3_5/lists/apps.py | 5 + .../chap3_5/lists/migrations/__init__.py | 0 src/chap3/chap3_5/lists/models.py | 3 + src/chap3/chap3_5/lists/tests.py | 9 ++ src/chap3/chap3_5/lists/views.py | 4 + src/chap3/chap3_5/manage.py | 22 ++++ src/chap3/chap3_5/superlists/__init__.py | 0 src/chap3/chap3_5/superlists/settings.py | 120 ++++++++++++++++++ src/chap3/chap3_5/superlists/urls.py | 21 +++ src/chap3/chap3_5/superlists/wsgi.py | 16 +++ src/chap3/chap3_6/manage.py | 22 ++++ src/chap3/chap3_6/superlists/__init__.py | 0 src/chap3/chap3_6/superlists/settings.py | 120 ++++++++++++++++++ src/chap3/chap3_6/superlists/urls.py | 21 +++ src/chap3/chap3_6/superlists/wsgi.py | 16 +++ textbook/chap3.md | 32 ++++- 18 files changed, 410 insertions(+), 4 deletions(-) create mode 100644 src/chap3/chap3_5/lists/__init__.py create mode 100644 src/chap3/chap3_5/lists/admin.py create mode 100644 src/chap3/chap3_5/lists/apps.py create mode 100644 src/chap3/chap3_5/lists/migrations/__init__.py create mode 100644 src/chap3/chap3_5/lists/models.py create mode 100644 src/chap3/chap3_5/lists/tests.py create mode 100644 src/chap3/chap3_5/lists/views.py create mode 100755 src/chap3/chap3_5/manage.py create mode 100644 src/chap3/chap3_5/superlists/__init__.py create mode 100644 src/chap3/chap3_5/superlists/settings.py create mode 100644 src/chap3/chap3_5/superlists/urls.py create mode 100644 src/chap3/chap3_5/superlists/wsgi.py create mode 100755 src/chap3/chap3_6/manage.py create mode 100644 src/chap3/chap3_6/superlists/__init__.py create mode 100644 src/chap3/chap3_6/superlists/settings.py create mode 100644 src/chap3/chap3_6/superlists/urls.py create mode 100644 src/chap3/chap3_6/superlists/wsgi.py diff --git a/src/chap3/chap3_5/lists/__init__.py b/src/chap3/chap3_5/lists/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/chap3/chap3_5/lists/admin.py b/src/chap3/chap3_5/lists/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/src/chap3/chap3_5/lists/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/src/chap3/chap3_5/lists/apps.py b/src/chap3/chap3_5/lists/apps.py new file mode 100644 index 0000000..efe43f0 --- /dev/null +++ b/src/chap3/chap3_5/lists/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class ListsConfig(AppConfig): + name = 'lists' diff --git a/src/chap3/chap3_5/lists/migrations/__init__.py b/src/chap3/chap3_5/lists/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/chap3/chap3_5/lists/models.py b/src/chap3/chap3_5/lists/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/src/chap3/chap3_5/lists/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/src/chap3/chap3_5/lists/tests.py b/src/chap3/chap3_5/lists/tests.py new file mode 100644 index 0000000..0b821f6 --- /dev/null +++ b/src/chap3/chap3_5/lists/tests.py @@ -0,0 +1,9 @@ +from django.urls import resolve +from django.test import TestCase +from lists.views import home_page + +class HomePageTest(TestCase): + + def test_root_url_resolves_to_home_page_view(self): + found = resolve('/') + self.assertEqual(found.func, home_page) \ No newline at end of file diff --git a/src/chap3/chap3_5/lists/views.py b/src/chap3/chap3_5/lists/views.py new file mode 100644 index 0000000..f5ed220 --- /dev/null +++ b/src/chap3/chap3_5/lists/views.py @@ -0,0 +1,4 @@ +from django.shortcuts import render + +# Create your views here. +home_page = None \ No newline at end of file diff --git a/src/chap3/chap3_5/manage.py b/src/chap3/chap3_5/manage.py new file mode 100755 index 0000000..3e6d019 --- /dev/null +++ b/src/chap3/chap3_5/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "superlists.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/src/chap3/chap3_5/superlists/__init__.py b/src/chap3/chap3_5/superlists/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/chap3/chap3_5/superlists/settings.py b/src/chap3/chap3_5/superlists/settings.py new file mode 100644 index 0000000..58530b5 --- /dev/null +++ b/src/chap3/chap3_5/superlists/settings.py @@ -0,0 +1,120 @@ +""" +Django settings for superlists 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 = '*2g@&(o7elmv%%3ar%))=v6d9*yr-c62-=x^rv#*#$)@pv$r*-' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + '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 = 'superlists.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 = 'superlists.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/src/chap3/chap3_5/superlists/urls.py b/src/chap3/chap3_5/superlists/urls.py new file mode 100644 index 0000000..5218133 --- /dev/null +++ b/src/chap3/chap3_5/superlists/urls.py @@ -0,0 +1,21 @@ +"""superlists 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 +from django.contrib import admin + +urlpatterns = [ + url(r'^admin/', admin.site.urls), +] diff --git a/src/chap3/chap3_5/superlists/wsgi.py b/src/chap3/chap3_5/superlists/wsgi.py new file mode 100644 index 0000000..e2d9e59 --- /dev/null +++ b/src/chap3/chap3_5/superlists/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for superlists 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", "superlists.settings") + +application = get_wsgi_application() diff --git a/src/chap3/chap3_6/manage.py b/src/chap3/chap3_6/manage.py new file mode 100755 index 0000000..3e6d019 --- /dev/null +++ b/src/chap3/chap3_6/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "superlists.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/src/chap3/chap3_6/superlists/__init__.py b/src/chap3/chap3_6/superlists/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/chap3/chap3_6/superlists/settings.py b/src/chap3/chap3_6/superlists/settings.py new file mode 100644 index 0000000..77a0a04 --- /dev/null +++ b/src/chap3/chap3_6/superlists/settings.py @@ -0,0 +1,120 @@ +""" +Django settings for superlists 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 = 'u%&)mznxwwe=w9qf&2av0)iskzif&sncrq5un6@9e$r908&66)' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + '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 = 'superlists.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 = 'superlists.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/src/chap3/chap3_6/superlists/urls.py b/src/chap3/chap3_6/superlists/urls.py new file mode 100644 index 0000000..5218133 --- /dev/null +++ b/src/chap3/chap3_6/superlists/urls.py @@ -0,0 +1,21 @@ +"""superlists 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 +from django.contrib import admin + +urlpatterns = [ + url(r'^admin/', admin.site.urls), +] diff --git a/src/chap3/chap3_6/superlists/wsgi.py b/src/chap3/chap3_6/superlists/wsgi.py new file mode 100644 index 0000000..e2d9e59 --- /dev/null +++ b/src/chap3/chap3_6/superlists/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for superlists 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", "superlists.settings") + +application = get_wsgi_application() diff --git a/textbook/chap3.md b/textbook/chap3.md index 391d229..00b60f5 100644 --- a/textbook/chap3.md +++ b/textbook/chap3.md @@ -47,11 +47,35 @@ python manage.py test ## 3.4 Django's MVC, URLs, and View Functions +Django is structured along a classic [**Model-View-Controller (MVC)** pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller). -## At Last! We Actually Write Some Application Code! +Django's main job is to decide what to do when a user asks for a particular URL on our site. -## urls.py +Django's workflow is like: -## Unit Testing a View +1. An HTTP `request` comes in for a particular URL +2. *Resolving the URL*: Django uses some rules to decide which `view` function should deal with the request. +3. The view function processes the request and returns an HTTP `response` -### The Unit-Test/Code Cycle \ No newline at end of file +Let's design a tests: + +1. Verify we can resolve the URL for the root of site ("/") to a particular view function we've made? +2. Verify view function return some HTML which will get the functional test to pass? + +## 3.5 At Last! We Actually Write Some Application Code! + +Add content in `lists/views.py`, to pass the test + +```python +from django.shortcuts import render + +home_page = None +``` + +## 3.6 urls.py + + + +## 3.7 Unit Testing a View + +### 3.7.1 The Unit-Test/Code Cycle \ No newline at end of file