Django(十一)后台管理系统的搭建

  • Post author:
  • Post category:其他




1.将后台管理系统转换成中文

将settings.py进行修改

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
# 中文
LANGUAGE_CODE = 'zh-hans'           
# 时区:上海
TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

效果:

在这里插入图片描述



2.在网上下载Xadmin程序,并且将文件复制到一级目录下

下载xadmin2,下载完成完成删除,保留和xadmin2一同下载的东西;

参考:

https://www.cnblogs.com/mxsf/p/10661626.html



3.注册邮箱验证码表格


分别在users,organiztion,operation,operation,course下新建adminx.py文件

新建过程:右击三级目录(users,organiztion,operation,operation,course目录,new->python file)




users.adminx.py

# _*_ encoding:utf-8 _*_
___author___ = 'boddy'
___date___ = '2019/4/9 13:40'

import xadmin
from .models import EmailVerifyRecord,Banner

#新建对model进行管理的类,继承最底层的类
class EmailVerifyRecordAdmin(object):
    #列表中默认显示的项目
    list_display = ['code', 'email', 'send_type', 'send_time']
    #搜索包含的内容
    search_fields = ['code', 'email', 'send_type']
    # 检索
    list_filter = ['code', 'email', 'send_type','send_time']
    pass


class BannerAdmin(object):
    #列表中默认显示的项目
    list_display = ['title', 'image', 'url', 'index','add_time']
    #搜索包含的内容
    search_fields = ['title', 'image', 'url', 'index']
    # 检索
    list_filter = ['title', 'image', 'url', 'index','add_time']


xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin)
xadmin.site.register(Banner,BannerAdmin)



courses.admin.py

# _*_ encoding:utf-8 _*_
___author___ = 'boddy'
___date___ = '2019/4/9 15:46'

import xadmin
from .models import Course,Lesson,Video,CourseResource

class CourseAdmin(object):
    list_display = ['name', 'desc', 'detail', 'degree','learn_times', 'students', 'fav_nums', 'image', 'click_nums']
    #搜索包含的内容
    search_fields = ['name', 'desc', 'detail', 'degree','learn_times', 'students', 'fav_nums', 'image', 'click_nums']
    # 检索
    list_filter = ['name', 'desc', 'detail', 'degree','learn_times', 'students', 'fav_nums', 'image', 'click_nums']

class LessonAdmin(object):
    #列表中默认显示的项目
    list_display = ['course', 'name','add_time']
    #搜索包含的内容
    search_fields = ['course', 'name']
    # 检索
    list_filter =    ['course', 'name','add_time']

class VideoAdmin(object):
    #列表中默认显示的项目
    list_display = ['lesson', 'name','add_time']
    #搜索包含的内容
    search_fields = ['lesson', 'name']
    # 检索
    list_filter =    ['lesson', 'name','add_time']

class CourseResourceAdmin(object):
    #列表中默认显示的项目
    list_display = ['course', 'name','download','add_time']
    #搜索包含的内容
    search_fields = ['course', 'name','download']
    # 检索
    list_filter =   ['course', 'name','download','add_time']


xadmin.site.register(Course,CourseAdmin)
xadmin.site.register(Lesson,LessonAdmin)
xadmin.site.register(Video,VideoAdmin)
xadmin.site.register(CourseResource,CourseResourceAdmin)



operation.admin.py

# _*_ encoding:utf-8 _*_
___author___ = 'boddy'
___date___ = '2019/4/9 15:27'

import xadmin
from .models import UserAsk,CourseComments,UserFavorite,UserMessage,UserCourse

class UserAskAdmin(object):
    #列表中默认显示的项目
    list_display = ['name', 'mobile', 'course_name', 'add_time']
    #搜索包含的内容
    search_fields = ['name', 'mobile', 'course_name']
    # 检索
    list_filter = ['name', 'mobile', 'course_name', 'add_time']


class CourseCommentsAdmin(object):
    #列表中默认显示的项目
    list_display = ['user', 'course', 'comments', 'add_time']
    #搜索包含的内容
    search_fields = ['user', 'course', 'comments']
    # 检索
    list_filter = ['user', 'course', 'comments', 'add_time']


class UserFavoriteAdmin(object):
    #列表中默认显示的项目
    list_display = ['user', 'fav_id', 'fav_type', 'add_time']
    #搜索包含的内容
    search_fields = ['user', 'fav_id', 'fav_type']
    # 检索
    list_filter = ['user', 'fav_id', 'fav_type', 'add_time']


class UserMessageAdmin(object):
    #列表中默认显示的项目
    list_display = ['user', 'message', 'has_read', 'add_time']
    #搜索包含的内容
    search_fields = ['user', 'message', 'has_read']
    # 检索
    list_filter = ['user', 'message', 'has_read', 'add_time']


class UserCourseAdmin(object):
    #列表中默认显示的项目
    list_display = ['user', 'course', 'add_time']
    #搜索包含的内容
    search_fields = ['user', 'course']
    # 检索
    list_filter = ['user', 'course', 'add_time']


xadmin.site.register(UserAsk,UserAskAdmin)
xadmin.site.register(CourseComments,CourseCommentsAdmin)
xadmin.site.register(UserFavorite,UserFavoriteAdmin)
xadmin.site.register(UserMessage,UserMessageAdmin)
xadmin.site.register(UserCourse,UserCourseAdmin)



organization.admin.py

# _*_ encoding:utf-8 _*_
___author___ = 'boddy'
___date___ = '2019/4/9 15:14'

import xadmin
from .models import CityDict,CourseOrg,Teacher

class CityDictAdmin(object):
    #列表中默认显示的项目
    list_display = ['name', 'desc', 'add_time']
    #搜索包含的内容
    search_fields = ['name', 'desc']
    # 检索
    list_filter = ['name', 'desc', 'add_time']


class CourseOrgAdmin(object):
    #列表中默认显示的项目
    list_display = ['name', 'desc','clik_num', 'fav_nums','image','address','city','add_time']
    #搜索包含的内容
    search_fields = ['name', 'desc','clik_num', 'fav_nums','image','address','city']
    # 检索
    list_filter = ['name', 'desc','clik_num', 'fav_nums','image','address','city','add_time']


class TeacherAdmin(object):
    #列表中默认显示的项目
    list_display = ['org', 'name','work_years', 'work_company','work_position','points','clik_num','fav_nums','add_time']
    #搜索包含的内容
    search_fields = ['org', 'name','work_years', 'work_company','work_position','points','clik_num','fav_nums']
    # 检索
    list_filter = ['org', 'name','work_years', 'work_company','work_position','points','clik_num','fav_nums','add_time']



xadmin.site.register(CityDict,CityDictAdmin)
xadmin.site.register(CourseOrg,CourseOrgAdmin)
xadmin.site.register(Teacher,TeacherAdmin)

效果:

在这里插入图片描述



4.1 新建课程未显示课程

在这里插入图片描述

将代码进行如下修改:

# Create your models here.
class UserProfile(AbstractUser):
    nick_name = models.CharField(max_length=50,verbose_name=u"昵称",default=" ")
    birday = models.DateField(verbose_name=u"生日",null=True,blank=True)
    gender = models.CharField(max_length=10,choices=(("male",u"男"),("female","女")),default="female")
    address = models.CharField(max_length=100,default=u"")
    mobile = models.CharField(max_length=11,null=True,blank=True)
    image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png",max_length=100)

    class Meta:
        verbose_name= u"用户信息"
        verbose_name_plural=verbose_name

    def __str__(self):
        return self.username

注:教程里面使用的是**__ unicode

()

,如果使用的是python的话使用:


str __()**


https://blog.csdn.net/feifashengwu/article/details/12625719



5.实现“主题”选择,修改页面风格




user.adminx.py

from xadmin import views

#实现主题选择
class BaseSetting(object):
    enable_themes = True
    use_bootswatch = True
    
 
xadmin.site.register(views.BaseAdminView,BaseSetting)

效果

在这里插入图片描述



6.修改“xadmin”标题,以及“我的公司”页脚




user.adminx.py

#修改标题,以及网页页脚
class GlobalSettings(object):
    site_title = "慕学后台管理系统"
    site_footer = "慕学在线网"
 
 xadmin.site.register(views.CommAdminView,GlobalSettings)

效果:

在这里插入图片描述



7.将网页中的选项折叠



user.adminx.py将6中的代码进行如下修改
#修改标题,以及网页页脚,对标签进行折叠
class GlobalSettings(object):
    site_title = "慕学后台管理系统"
    site_footer = "慕学在线网"
    menu_style = "accordion"
 
 xadmin.site.register(views.CommAdminView,GlobalSettings)

效果:

在这里插入图片描述



8.将显示英文的标签改成中文:



course



courses.apps.py全部代码
# _*_ encoding:utf-8 _*_
from django.apps import AppConfig


class CoursesConfig(AppConfig):
    name = 'courses'
    verbose_name = u"课程管理"


courses.__ init __.py全部代码
default_app_config = "courses.apps.CoursesConfig"



operation



operation.apps.py全部代码
# _*_ encoding:utf-8 _*_
from django.apps import AppConfig


class OperationConfig(AppConfig):
    name = 'operation'
    verbose_name = u"用户操作"


operation.__ init __.py全部代码
default_app_config = "operation.apps.OperationConfig"



organization



organization.apps.py全部代码
# _*_ encoding:utf-8 _*_
from django.apps import AppConfig


class OrganizationConfig(AppConfig):
    name = 'organization'
    verbose_name = u"机构管理"


organization.__ init __.py全部代码
default_app_config = "organization.apps.OrganizationConfig"



USER



USER.apps.py全部代码
# _*_ encoding:utf-8 _*_
from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = 'users'
    verbose_name = u"用户信息"


USER.__ init __.py全部代码
default_app_config = "users.apps.UsersConfig"

效果:

在这里插入图片描述



版权声明:本文为qq_41068712原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。