1.管理数据表
(1)修改admin.py文件
1 from .models import Grades,Students 2 #注册 3 admin.site.register(Grades) 4 admin.site.register(Students)
(2)自定义管理页面
1 from django.contrib import admin 2 3 # Register your models here.在这里注册你的模型 4 from .models import Grades,Students 5 #注册 6 class GradesAdmin(admin.ModelAdmin): 7 #列表页的属性 8 list_display = ['pk','gname','gdate','ggirlnum','gboynum','isDelete']#显示字段 9 list_filter = ['gname']#过滤字段 10 search_fields = ['gname']#搜索字段 11 list_per_page = 5#分页 12 #添加页、修改页属性 13 #fields = ['ggirlnum','gboynum','gname','gdate','isDelete']#规定属性的先后顺序 14 fieldsets = [ 15 ("num",{"fields":['ggirlnum','gboynum']}), 16 ("base", {"fields": ['gname','gdate','isDelete']}), 17 ]#给属性分组 18 #fields和fieldsets两个属性不可以同时使用 19 admin.site.register(Grades,GradesAdmin) 20 admin.site.register(Students)
关联对象要求:在创建一个班级的时候可以直接添加几个学生
1 from .models import Grades,Students 2 #注册 3 #添加关联对象 4 class StudentsInfo(admin.TabularInline): #StackedInline可以替换TabularInline区别是添加页面排版不同
5 model = Students#添加学生个人信息 6 extra = 2#添加两个 7 class GradesAdmin(admin.ModelAdmin): 8 inlines = [StudentsInfo]#与学生关联 9 #列表页的属性 10 list_display = ['pk','gname','gdate','ggirlnum','gboynum','isDelete']#显示字段 11 list_filter = ['gname']#过滤字段 12 search_fields = ['gname']#搜索字段
运行如图所示:
布尔值显示问题
1 class StudentsAdim(admin.ModelAdmin): 2 def gender(self):#把布尔值显示为文字 3 if self.sgender: 4 return "男" 5 else: 6 return "女" 7 #设置页面列的名称 8 gender.short_description = "性别"
list_display = ['pk','sname','sage',gender,'scontend','sgrade','isDelete']
执行动作位置问题
1 class StudentsAdim(admin.ModelAdmin): 2 def gender(self):#把布尔值显示为文字 3 if self.sgender: 4 return "男" 5 else: 6 return "女" 7 #设置页面列的名称 8 gender.short_description = "性别" 9 10 list_display = ['pk','sname','sage',gender,'scontend','sgrade','isDelete'] 11 list_per_page = 10 12 #执行动作的位置 13 actions_on_top = False 14 actions_on_bottom = True 15 admin.site.register(Students,StudentsAdim)
补充此时admin.py中的完整代码为
1 from django.contrib import admin 2 3 # Register your models here.在这里注册你的模型 4 from .models import Grades,Students 5 #注册 6 #添加关联对象 7 class StudentsInfo(admin.TabularInline): 8 model = Students#添加学生个人信息 9 extra = 2#添加两个 10 class GradesAdmin(admin.ModelAdmin): 11 inlines = [StudentsInfo]#与学生关联 12 #列表页的属性 13 list_display = ['pk','gname','gdate','ggirlnum','gboynum','isDelete']#显示字段 14 list_filter = ['gname']#过滤字段 15 search_fields = ['gname']#搜索字段 16 list_per_page = 5#分页 17 #添加页、修改页属性 18 #fields = ['ggirlnum','gboynum','gname','gdate','isDelete']#规定属性的先后顺序 19 fieldsets = [ 20 ("num",{"fields":['ggirlnum','gboynum']}), 21 ("base", {"fields": ['gname','gdate','isDelete']}), 22 ]#给属性分组 23 #fields和fieldsets两个属性不可以同时使用 24 admin.site.register(Grades,GradesAdmin) 25 26 class StudentsAdim(admin.ModelAdmin): 27 def gender(self):#把布尔值显示为文字 28 if self.sgender: 29 return "男" 30 else: 31 return "女" 32 #设置页面列的名称 33 gender.short_description = "性别" 34 35 list_display = ['pk','sname','sage',gender,'scontend','sgrade','isDelete'] 36 list_per_page = 10 37 #执行动作的位置 38 actions_on_top = False 39 actions_on_bottom = True 40 admin.site.register(Students,StudentsAdim)
使用装饰器完成注册
1 @admin.register(Students) 2 class StudentsAdim(admin.ModelAdmin): 3 def gender(self):#把布尔值显示为文字 4 if self.sgender: 5 return "男" 6 else: 7 return "女" 8 #设置页面列的名称 9 gender.short_description = "性别" 10 11 list_display = ['pk','sname','sage',gender,'scontend','sgrade','isDelete'] 12 list_per_page = 10 13 #执行动作的位置 14 actions_on_top = False 15 actions_on_bottom = True 16 #admin.site.register(Students,StudentsAdim)
2.视图的基本使用
定义视图在view.py中写
1 from django.http import HttpResponse 2 def index(request): 3 return HttpResponse("sunck si a good man")
配置url
修改project目录下的url.py文件
1 from django.contrib import admin 2 from django.urls import path,include 3 4 urlpatterns = [ 5 path('admin/', admin.site.urls), 6 path(r'',include('myApp.urls')), 7 8 ]
在myApp应用目录下创建一个url.py文件
1 from django.conf.urls import url 2 from . import views 3 urlpatterns = [ 4 url(r'^$',views.index) 5 ]
view.py
1 from django.http import HttpResponse 2 def index(request): 3 return HttpResponse("sunck si a good man") 4 def detail(request,num,num2): 5 return HttpResponse("detail-%s-%s"%(num,num2))
url.py
1 from django.conf.urls import url 2 from . import views 3 urlpatterns = [ 4 url(r'^$',views.index),#可以匹配http://127.0.0.1:8000/ 5 url(r'^(d+)/(d+)/$',views.detail)#可以匹配http://127.0.0.1:8000/数字/数字/ 6 ]
3.模板的使用
模板是HTML页面,可以根据视图中传递过来的数据进行填充
创建模板
创建templates目录,在目录下创建对应项目的模板目录(project/templates/myApp)
配置模板路径
修改settings.py文件
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR,'templates')], 'APP_DIRS': True, 'OPTIONS': {
在templates/myApp下创建两个html模板即grades.html和students.html
模板语法:
{{输出值,可以是变量,也可以是对象,属性}}
{%执行代码段%}
用 http://127.0.0.1:8000/grades打开写模板文件
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>班级信息</title> 6 </head> 7 <body> 8 <h1>班级信息列表</h1> 9 <u1> 10 <!--[python04,python05,python06]--> 11 {%for grade in grades%} 12 <li> 13 <a href="="#>{{grade.gname}}</a> 14 </li> 15 {%endfor%} 16 </u1> 17 </body> 18 </html>