• Django学习系列之Python+Xadmin


    项目树

    引入xadmin 

    pycharm在项目中创建存放xadmin的目录

    右键项目名称-->pythonpackage-->输入名称:extra_app

    拷贝xadmin代码到extra_app

    (mxonline) zhaijunming@zhaijuningdeMBP:~/Desktop/Project/python$ git clone  https://github.com/zaxlct/imooc-django.git
    (mxonline) zhaijunming@zhaijuningdeMBP:~/Desktop/Project/python$ cp -r imooc-django/extra_apps/xadmin   MxOnline/extra_apps/.

    配置settings.py

    # 设置 apps, extra_apps 目录
    sys.path.insert(0,os.path.join(BASE_DIR,'apps'))
    sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
    
    #添加xadmin app
    INSTALLED_APPS = [
        'xadmin',
        'crispy_forms',
    ]
    
    
    LANGUAGE_CODE = 'zh-hans'
    
    TIME_ZONE = 'Asia/Shanghai'
    
    USE_I18N = True
    
    USE_L10N = True
    
    USE_TZ = False

    配置url

    MxOnline/urls.py

    from django.conf.urls import url
    from django.contrib import admin
    import xadmin
    
    urlpatterns = [
        url(r'^admin/', xadmin.site.urls),
    ]

    生成数据表

    makemigrations
    migrate

    测试

    访问127.0.0.1:8000/admin

    页头和页尾自定义 

    随便一个项目的adminx.py中增加以下代码

    from xadmin import views
     
    class GlobalSetting(object):
        site_title = "慕学网后台管理系统"
        site_footer = "慕学在线网"
     
    xadmin.site.register(views.CommAdminView, GlobalSetting)

    左侧菜单栏样式改变

    修改前

    修改后

    随便一个项目的adminx.py中增加以下代码

    from xadmin import views
     
    class GlobalSetting(object):
        site_title = "慕学网后台管理系统"
        site_footer = "慕学在线网"
        menu_style = "accordion"
     
    xadmin.site.register(views.CommAdminView, GlobalSetting)

     左侧菜单栏app名称改成中文

     修改前

    修改后

    每一个app同理

    比如users这个app

    users-->apps.py

    from django.apps import AppConfig
    
    
    class UsersConfig(AppConfig):
        name = 'users'
        verbose_name = '用户信息'  增加这个字段,这个就是要显示的名称

    users-->init.py

    default_app_config = "users.apps.UsersConfig"

    将models注册进xadmin

    表结构(models.py)

    from django.db import models
    from datetime import datetime
    from organization.models import *
    # Create your models here.
    #这里有多个表,CourseOrg(机构表),course(课程表),lesson(章节表),Video(视频表),机构和课程是一对多的关系,一个机构有多个课程,课程和章节是一对多的关系,一个课程有多个章节,章节和视频也是一对多关系,一个章节有多个视频
    
    

    class CourseOrg(models.Model):
    '''机构'''
    name = models.CharField(max_length=50,verbose_name='机构名称')
    choice_category = (
    ('pxjg', '培训机构'),
    ('gx', '高校'),
    ('gr', '个人'),
    )
    category = models.CharField(default='pxjg',max_length=20,choices=choice_category)
    desc = models.TextField(verbose_name='机构描述')
    click_num = models.IntegerField(default=0,verbose_name='点击数')
    fav_num = models.IntegerField(default=0,verbose_name='收藏数')
    image = models.ImageField(upload_to='org/%Y/%m',verbose_name='logo')
    address = models.CharField(max_length=150,verbose_name='机构地址')
    city = models.ForeignKey(CityDict,verbose_name='机构所在城市')
    student = models.IntegerField(default=0,verbose_name='学习人数')
    course_nums = models.IntegerField(default=0,verbose_name='课程数')
    add_time = models.DateTimeField(default=datetime.now)

    class Meta:
    verbose_name = '课程机构'
    verbose_name_plural = '课程机构'

    def __str__(self):
    return self.name

    class Course(models.Model): '''课程''' course_org = models.ForeignKey(CourseOrg,verbose_name='课程机构',null=True,blank=True) name = models.CharField(max_length=50,verbose_name='课程名') desc = models.CharField(max_length=300,verbose_name='课程描述') detail = models.TextField(verbose_name='课程详情') choice_degree = ( ('cj','初级'), ('zj','中级'), ('gj','高级'), ) degree = models.CharField(choices=choice_degree,max_length=2,verbose_name='课程难度') learn_times = models.IntegerField(default=0,verbose_name='学习时长(分钟数)') students = models.IntegerField(default=0,verbose_name='学习人数') fav_nums = models.IntegerField(default=0,verbose_name='收藏人数') image = models.ImageField(upload_to='courses/%Y/%m',verbose_name='封面图') click_nums = models.IntegerField(default=0,verbose_name='点击数') add_time = models.DateTimeField(default=datetime.now,verbose_name='添加时间') class Meta: verbose_name = '课程' verbose_name_plural = '课程' def __str__(self): return self.name class Lesson(models.Model): '''章节''' course = models.ForeignKey(Course,verbose_name='课程') name = models.CharField(max_length=100,verbose_name='章节名称') add_time = models.DateTimeField(default=datetime.now,verbose_name='添加时间') class Meta: verbose_name = '章节' verbose_name_plural = '章节' def __str__(self): return self.name class Video(models.Model): '''视频''' lesson = models.ForeignKey(Lesson,verbose_name='章节') name = models.CharField(max_length=100,verbose_name='视频名称') add_time = models.DateTimeField(default=datetime.now,verbose_name='添加时间') class Meta: verbose_name = '视频' verbose_name_plural = '视频' def __str__(self): return self.name

    adminx.py

    import xadmin
    from xadmin import views
    
    
    from .models import  *
    
    class CourseAdmin(object):
        list_display = ['course_org','name', 'desc', 'detail', 'degree','learn_times','students','fav_nums','image','click_nums','add_time']
        search_fields = ['course_org','name', 'desc', 'detail', 'degree','students','fav_nums','image','click_nums']
        list_filter = ['course_org__name','name', 'desc', 'detail', 'degree','learn_times','students','fav_nums','image','click_nums','add_time']
    
    
    
    class LessonAdmin(object):
        list_display = ['course', 'name','add_time']
        search_fields = ['course', 'name']
        #由于lesson和course是一对多关系,一个课程有多个章节,这里的course是course 的外键,想要根据课程名称过滤章节的话,需要指定course中的一个字段,比如说name,就是根据课程名过滤属于这个课程名称的所有章节
        list_filter = ['course__name', 'name','add_time']
    
    
    class VideoAdmin(object):
        list_display  = ['lesson','name','add_time']
        search_fields = ['lesson','name']
        list_filter   = ['lesson__name', 'name', 'add_time']
    
    
    
    
    
    xadmin.site.register(Course,CourseAdmin)
    xadmin.site.register(Lesson,LessonAdmin)
    xadmin.site.register(Video,VideoAdmin)
    #我们把course,lesson,video注册进xadmin了

    效果:

     

  • 相关阅读:
    .net core 2.2, new Bitmap出错 The type initializer for 'Gdip' threw an exception
    瑞萨单片机学习笔记(待续)
    Linux-GitLab安装及汉化
    mysql5.7 ibtmp1文件过大
    #和$的区别
    RTP封装h264
    一个项目同时需要向两个地址推送
    git命令
    echart绘制进度条、仪表盘、各种样式的折线图、饼图、环形图、地图等
    前端在实现类似控制台命令行或者告警信息提示时,需要保持滚动条始终停留在最新的信息位置,也就是最底部
  • 原文地址:https://www.cnblogs.com/chadiandianwenrou/p/7526396.html
Copyright © 2020-2023  润新知