Finished 3. Django admin
parent
5a413f7e57
commit
79dc2c3982
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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)
|
BIN
src/db.sqlite3
BIN
src/db.sqlite3
Binary file not shown.
Loading…
Reference in New Issue