Finished 3. Django admin

master
Jason Zhu 2020-10-19 15:41:33 +11:00
parent 5a413f7e57
commit 79dc2c3982
3 changed files with 40 additions and 2 deletions

View File

@ -179,3 +179,40 @@ After creating new model, we need to add it into database:
1. Let Django know there is change: `python manage.py makemigrations blog` 1. Let Django know there is change: `python manage.py makemigrations blog`
1. So, Django will prepare a migration file 1. So, Django will prepare a migration file
2. After prepared migration file `blog/migrations/0001_initial.py`, apply it to database: `python manage.py migrate blog` 2. After prepared migration file `blog/migrations/0001_initial.py`, apply it to database: `python manage.py migrate blog`
## 3. Django admin
To adding/editing posts, need use Django admin
### 3.1 Add Post models into admin site
Edit `blog/admin.py`
```python
from django.contrib import admin
from .models import Post
admin.site.register(Post)
```
### 3.2 Add superuser to get access
```
python manage.py createsuperuser
```
### 3.3 Add/Edit post in admin site
Start webserver `python manage.py runserver` then go to http://127.0.0.1/admin
In the post, we can find it has multiple attributes/members including:
* `author`
* `Title`
* `Text`
* `Created date`
* `Published date`
They are all forms that we created in Post model
## 4. Deploy (Omit)

View File

@ -1,3 +1,4 @@
from django.contrib import admin from django.contrib import admin
from .models import Post
# Register your models here. admin.site.register(Post)

Binary file not shown.