Created first polls app

master
Jason Zhu 2020-10-18 20:07:45 +11:00
parent 7e24d15a79
commit 67bf818034
9 changed files with 90 additions and 2 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
.vscode .vscode
**__pycache__**
**db.sqlite3**

View File

@ -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 * **public site**: let's pp view polls and vote
* **admin site**: let's you(admin) add/change/delete poll * **admin site**: let's you(admin) add/change/delete poll
# Creating a project ## Creating a project
In the source directory, create project by
```
$ django-admin startproject <project_name>
```
Constraint on `<project_name>`:
* 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
>
> Whats 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
```

View File

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class PollsConfig(AppConfig):
name = 'polls'

View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,2 @@
from django.http import request
from django.shortcuts import render