1、运行django的指令:python manage.py runserver 2、创建项目 django-admin.py startproject mesite 3、创建应用 django-admin.py startapp blog 4、注册应用 settings INSTALLED_APPS 5、根据数据库模型建立数据库表文件(相当于生成sql语句) python manage.py makemigrations 创建数据库表 python manage.py migrate 6、创建超级管理员 python manage.py createsuperuser 登录: 127.0.0.1:8000/admin 7、带环境的shell python manage.py shell 8、获取BookInfo.objects.all() BookInfo.objects.get(pk=1) 9、把模型类注册到admin管理界面 admin.site.register(BookInfo) 10、fields和fieldsets是一个东西 保留一个就可以了 class BookInfoAdmin(admin.ModelAdmin): # 列表页 #显示字段 点击列头可排序 list_display = ["pk", "btitle", 'bpub_date'] #过滤字段,过滤框会出现在右侧 list_filter = ["btitle"] #搜索字段,搜索框会出现在上侧 search_fields = ['btitle'] #分页, 分页框出现在下侧 list_per_page = 5 #修改页和添加页 # 属性分组 fieldsets = [ (u"基础信息", {"fields": ['btitle']}), (u"时间信息", {"fields": ['bpub_date']}), ] class HeroInfoAdmin(admin.ModelAdmin): list_display = ["hname", "hgender", "hcontent", "hbook"] list_filter = ["hname"] search_fields = ["hname"] list_per_page = 5 fieldsets = [ ("base", {"fields": ['hname', 'hgender']}), ("more", {"fields": ['hcontent', 'hbook']}), ] admin.site.register(BookInfo, BookInfoAdmin) admin.site.register(HeroInfo, HeroInfoAdmin) 11、关联注册 class HeroInfoInline(admin.TabularInline): model = HeroInfo extra = 3 class BookInfoAdmin(admin.ModelAdmin): inlines = [HeroInfoInline] 12、路由 from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^', include('booktest.urls')), ] 13、管理器是模型类的一个属性,帮助模型类进行数据库操作