2020-10-19 13:46:15 +11:00
|
|
|
# django girls blog
|
|
|
|
|
|
|
|
Target: create a blog using django.
|
2020-10-19 14:13:12 +11:00
|
|
|
|
|
|
|
## 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
|
|
|
|
|