• django的如何创建一个小例子-3


    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>
  • 相关阅读:
    在 Eclipse 中使用 JUnit4 进行单元测试
    版本控制之道使用Git
    程序员成熟的标志
    Java 编程需要注意的细节
    【netty】netty的websocketframe
    求一个数内含1个次数
    apache bench的使用
    更改centos epel源
    [linux]centos7下解决yum install mysqlserver没有可用包
    cacti的安装与配置
  • 原文地址:https://www.cnblogs.com/ZHANG576433951/p/12156604.html
Copyright © 2020-2023  润新知