生成同步数据库的脚本:
python manage.py makemigrations
同步数据库:
Python manage.py migrate
创建后台用户
python manage.py createsuperuser
访问 admin 界面:Http://127.0.0.1:8000/admin
后台展示 Book 表
在 admin.py 上修改
from Django.contrib import admin
from app01.models import *
# ReGISter your models here.
class Myadmin(admin.ModelAdmin):
list_display = ("title", "price", "publisher")
admin.site.register(Book, Myadmin)
修改为中文界面和设置时间
在 settings.py 中修改为
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
修改数据库展示名的字段名
在 models.py 字段后面添加 verbose_name,例如:
class Book(models.Model):
title = models.CharField(max_length=100, verbose_name="书名")
添加搜索框
在 admin.py 上修改
from djanGo.contrib import admin
from app01.models import *
# Register your models here.
class Myadmin(admin.ModelAdmin):
list_display = ("title", "price", "publisher")
search_fields = ("title",)
admin.site.register(Book, Myadmin)
添加过滤器
在 admin.py 上增加
class Myadmin(admin.ModelAdmin):
....
list_filter = ("publisher",)