From 67bf818034e1487ef3751b239ff0d19d9ff82962 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Sun, 18 Oct 2020 20:07:45 +1100 Subject: [PATCH] Created first polls app --- .gitignore | 4 +- first_django_app.md | 72 ++++++++++++++++++++++++++++- mysite/polls/__init__.py | 0 mysite/polls/admin.py | 3 ++ mysite/polls/apps.py | 5 ++ mysite/polls/migrations/__init__.py | 0 mysite/polls/models.py | 3 ++ mysite/polls/tests.py | 3 ++ mysite/polls/views.py | 2 + 9 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 mysite/polls/__init__.py create mode 100644 mysite/polls/admin.py create mode 100644 mysite/polls/apps.py create mode 100644 mysite/polls/migrations/__init__.py create mode 100644 mysite/polls/models.py create mode 100644 mysite/polls/tests.py create mode 100644 mysite/polls/views.py diff --git a/.gitignore b/.gitignore index 600d2d3..ca5f1be 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.vscode \ No newline at end of file +.vscode +**__pycache__** +**db.sqlite3** \ No newline at end of file diff --git a/first_django_app.md b/first_django_app.md index e53fcb8..09583d3 100644 --- a/first_django_app.md +++ b/first_django_app.md @@ -5,4 +5,74 @@ Target: create a basic poll application, with 2 parts a **public site** and a ** * **public site**: let's pp view polls and vote * **admin site**: let's you(admin) add/change/delete poll -# Creating a project \ No newline at end of file +## Creating a project + +In the source directory, create project by + +``` +$ django-admin startproject +``` + +Constraint on ``: +* don't use names that conflict with django + * e.g. `django`, `test` (test with built-in test file) + +``` +mysite/ + manage.py + mysite/ + __init__.py + settings.py + urls.py + asgi.py + wsgi.py +``` + +Explain file structure: +* top-level `mysite/`: container of the project, can be renamed latter w/o affect Django framework +* `manage.py`: cmd-line utility let us to interact (admin) with Django projects. +* inner `mysite/` dir: actual Python package for the project. + * Importing any package from here: e.g. `mysite.urls` +* `mysite/__init__.py`: empty file, tell python start from here. +* `mysite/settings.py`: settings/config for this django project. +* `mysite/urls.py`: URL declarations for this Django pjt; a "table of contents" of this Django website. +* `mysite/asgi.py`: An entry-point for ASGI-compatible webservers +* `mysite/wsgi.py`: Entry-point for WSGI-compatible webservers + +## The development server + +Start server steps: +1. cd into outer `mysite/` dir +2. Start server via: `python manage.py runserver` + +Outcome: + +* A development server is started. So no need to config a production server (e.g. Apache) +* Note: don't use it as production environment. + +## Creating the Polls app + +> Projects vs. apps +> +> What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a small poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects. + +Create a poll app along `mysite/`. For what? + +``` +$ python manage.py startapp polls +``` + +app hierarchy + +``` +polls/ + __init__.py + admin.py + apps.py + migrations/ + __init__.py + models.py + tests.py + views.py +``` + diff --git a/mysite/polls/__init__.py b/mysite/polls/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/polls/admin.py b/mysite/polls/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/mysite/polls/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. 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/__init__.py b/mysite/polls/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mysite/polls/models.py b/mysite/polls/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/mysite/polls/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. 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/views.py b/mysite/polls/views.py new file mode 100644 index 0000000..c53ddeb --- /dev/null +++ b/mysite/polls/views.py @@ -0,0 +1,2 @@ +from django.http import request +from django.shortcuts import render