From d609a518a90bb8f92112a0ff3ddd22cf6138cfe1 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Mon, 19 Oct 2020 14:13:12 +1100 Subject: [PATCH] Finished 1.0 Your first Django project! --- .gitignore | 1 + django-girls.md | 63 +++++++++++++++++++++ src/db.sqlite3 | Bin 0 -> 131072 bytes src/manage.py | 22 ++++++++ src/mysite/__init__.py | 0 src/mysite/asgi.py | 16 ++++++ src/mysite/settings.py | 122 +++++++++++++++++++++++++++++++++++++++++ src/mysite/urls.py | 21 +++++++ src/mysite/wsgi.py | 16 ++++++ 9 files changed, 261 insertions(+) create mode 100644 src/db.sqlite3 create mode 100755 src/manage.py create mode 100644 src/mysite/__init__.py create mode 100644 src/mysite/asgi.py create mode 100644 src/mysite/settings.py create mode 100644 src/mysite/urls.py create mode 100644 src/mysite/wsgi.py diff --git a/.gitignore b/.gitignore index 8b6e778..2cd63f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode env +**__pycache__** \ No newline at end of file diff --git a/django-girls.md b/django-girls.md index 354aa58..d36cdcf 100644 --- a/django-girls.md +++ b/django-girls.md @@ -1,3 +1,66 @@ # django girls blog Target: create a blog using django. + +## 01 Your first Django project! + +### 1.1 Create Django project + +* Command to start Django project: + +``` +$ django-admin startproject mysite. +``` + +Generating following dir structure: + +``` +djangogirls +├── manage.py +├── mysite +│ ├── __init__.py +│ ├── settings.py +│ ├── urls.py +│ └── wsgi.py +├── myvenv +│ └── ... +└── requirements.txt +``` + +where: +* `setting.py`L: helps with management of the site. + * contains configuration of the website +* `urls.py`: contains a list of patterns used by `urlresolve` + +### 1.2 Change settings of django website + +Edit `mysite/settings.py`: + +1. Change timezone: `TIME_ZONE=Australia/Canberra' +2. Change language: `LANGUAGE_CODE = 'en-us' +3. Add a path for static files (e.g. CSS and static files): `STATIC_ROOT = os.path.join(BASE_DIR, 'static')` +4. Add allowed host site: `ALLOWED_HOST = ['127.0.0.1', '.amazonaws.com']` for using cloud9 + +### 1.3 Set up a database + +Django support multiple databases, to store data. `sqlite3` is the default one, which is already set up as shown in `mysite/settings.py`: + +```python +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} +``` + +After confirming database settings, migrate the project `python manage.py migrate` + +### 1.4 Starting the web server + +``` +python manage.py runserver +``` + +## 2. Django models + diff --git a/src/db.sqlite3 b/src/db.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..71bbe8d21338e96cb4ff3a79527dc28084c2c72a GIT binary patch literal 131072 zcmeI5TWlNIdB-{Akkp0b(bc2I>lH<<)@HS`cpFmmHtlM?ima?x-u0y$0}ZA_awLz# zi%e3!AV5(nb`xyUJ|swh7D%=)1yZyRP1+z2Nq`_hQ6xp1wnz%}lA?Xuwm_1iMVp5< zK+idIhBF*el-9`tYvIpbAI&-EJKucY?_6fi1?08YE}Ly5zSC&d^maV!oA61J?}d2W z=kuMW|JYyqbBSKac7*m>n)?zG8^?)DwRrWX5DO?dNq^Eq*l`O z|6)9qy^zXZ$mY+d3u+;kzBC#1&Az^_@3(JJ`HZGl+eTB{Z_!}KY_{53U9TBhO~0>I zjrwl;)<8{pwU92pFhMnK+M3eZ{$53I8`@s8asPp~XEbZ3W!FFrYC4@yUnEq+E4GG| zuw}Q|*xx&r8QHZ|M%|22B{zhURMG8FRX?UbnQSJLdta0)x@Id{b1Pzaj>9FDrJt80 zRL=)(Jq4lXu3j}MpGLE#>6MCRG@Fg4R-;v??;5Q^h*E3mw3=TZr^>c$WqGY>+}Ssq zhE{IW+eW?Jez0e>G+OhSfhyFZs;<2_Mpe9Qt5Ds3aX{6^u36XW`_+N1CY8&l*Rn5+ zQXO_(r*du`T{exq_FzXcYB8O;I6^hpb)Cw(H5fJBtRB(isE4YWy%eTO?8ck28?V#W zj&3#T`C_41NaZhug1)8AbvH&vk@BrI?soBNj4xN(kC$r7r_^k2D;V@C)TT5(|o||SueL~-_&-h`mR>K)iB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j7--iw%f+dq78X#F|1^>(m~PvNOhMSU_-FC>IX zor$=6$5xo7S88URjknz$+p?O^86`dM6?jUx)%(_Rc=;aKT`}f58>rbvd$MzGKCEbT z6ZtiFOXTU^LQ@s|Q z8Q2E8;*Zw==yRn}*Vy)(`_U-Q7tu!BDBU*7ZAMnSfmu&Yk=!))=q{`M*v)u4G~E=e z?;2W-zQpRgRKNEX&I`mDeAwBhpVh!)w+u(SeR|4$hwiJ1zpmbpux#@&~Q_{B>b7hh&$fm{9;-y2UQlb!b~bW~fHI(M9B8aI~*&d}~X zl$YKqmDHU~xofo5(Od=I)7yFv7xWy?vo*V(W&;gkl$_gH9dGSaSSeFyZXV@~s1Z#s zs)k-kyUuZV{rhC;?!X^ zbeICKy_OtnPyC8ECG7tnHN!$a2!H?xfB*=900@8p2!H?xfB*q^|9)JJ{fB*=900@8p2!H?xfB*=900N*EH^F;%BBW>h(B_$3^^Eg~}w;A^KCI2L07-mz0&#l{;n0i61+3 z$yq73Tzr z8@WWxk`T7p62d4JeNU81oLQ2YF?Keyf?Yoyp?M3*0KYr`5xMT|c`Pwb^Osfv;{II; zVSptwX2}Q(x-!B99)D_-%FLe)FdMo8%m^MiH$rolvwm)c^DBE@Q=Fvd!c=m#;1~8d z8BA6fZCj)2YsVw)7)74L#^}&o|_N4 zrqYBsPdOK$Qp>6^)Ga5>bR_*;QW)u$6ee~h6B3nNT4e^h#h7_r8Jz$33|NSQ00@8p z2!H?xfB*=900@8p2!Ox|B!Kz<39MAO1Ogxc0w4eaAOHd&00JNY0w4ea9s>UlF-!L* literal 0 HcmV?d00001 diff --git a/src/manage.py b/src/manage.py new file mode 100755 index 0000000..a7da667 --- /dev/null +++ b/src/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + 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?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/src/mysite/__init__.py b/src/mysite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/mysite/asgi.py b/src/mysite/asgi.py new file mode 100644 index 0000000..451affb --- /dev/null +++ b/src/mysite/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for mysite project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') + +application = get_asgi_application() diff --git a/src/mysite/settings.py b/src/mysite/settings.py new file mode 100644 index 0000000..8921118 --- /dev/null +++ b/src/mysite/settings.py @@ -0,0 +1,122 @@ +""" +Django settings for mysite project. + +Generated by 'django-admin startproject' using Django 3.1.2. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +from pathlib import Path +import os + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = '74(wb#q%+tb&cocr0s$93!!sehz_risx9th_&p+nlq4=jm7lj*' + +# 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 = '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/3.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/3.1/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/3.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'Australia/Canberra' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = '/static/' +# STATIC_ROOT = os.path.join(BASE_DIR, 'static') diff --git a/src/mysite/urls.py b/src/mysite/urls.py new file mode 100644 index 0000000..e7f9f35 --- /dev/null +++ b/src/mysite/urls.py @@ -0,0 +1,21 @@ +"""mysite URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/src/mysite/wsgi.py b/src/mysite/wsgi.py new file mode 100644 index 0000000..d04765f --- /dev/null +++ b/src/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/3.1/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()