Django+xadmin打造在线教育平台
Posted huiyichanmian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django+xadmin打造在线教育平台相关的知识,希望对你有一定的参考价值。
一、xadmin后台管理
1.xadmin的安装(源码安装)
(1)直接git clone https://github.com/sshwsfc/xadmin.git(前提是装有git)
(2)新建Python Package "extra_apps",把源码xadmin文件夹放到extra_apps文件夹下面,此时目录结构如下:
(3)把extra_apps右键mark为Source Root
(4)在settings中加入如下代码
sys.path.insert(0,os.path.join(BASE_DIR, ‘extra_apps‘)
(5)配置路由,在MxOnlineMxOnlineurls.py中,把admin改成xdmin。
from django.urls import path import xadmin urlpatterns = [ path(‘xadmin/‘, xadmin.site.urls), ]
(6)注册app
把下面两个app注册到settings.py的INSTALLED_APPS中
‘xadmin‘, ‘crispy_forms‘
(7)重新生成数据库
python manage.py makemigrations
python manage.py migrate
(8)设置成中文(在settings文件中)
LANGUAGE_CODE = ‘zh-hans‘ TIME_ZONE = ‘Asia/Shanghai‘ USE_I18N = True USE_L10N = True USE_TZ = False
(8)创建一个管理员用户
python manage.py createsuperuser
密码不能太简单,email直接可以回车
(9)访问后台:http://127.0.0.1:8000/xadmin,登录。
2、users app的models注册
# users/adminx.py import xadmin from .models import EmailVerifyRecord #xadmin中这里是继承object,不再是继承admin 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‘]
xadmin.site.register(EmailVerifyRecord,EmailVerifyRecordAdmin)
users中Banner也注册进去
from .models import Banner 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(Banner,BannerAdmin)
3、course app的models注册
import xadmin from .models import Course, Lesson, Video, CourseResource class CourseAdmin(object): ‘‘‘课程‘‘‘ list_display = [ ‘name‘,‘desc‘,‘detail‘,‘degree‘,‘learn_times‘,‘students‘] search_fields = [‘name‘, ‘desc‘, ‘detail‘, ‘degree‘, ‘students‘] list_filter = [ ‘name‘,‘desc‘,‘detail‘,‘degree‘,‘learn_times‘,‘students‘] class LessonAdmin(object): ‘‘‘章节‘‘‘ list_display = [‘course‘, ‘name‘, ‘add_time‘] search_fields = [‘course‘, ‘name‘] #这里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‘, ‘add_time‘] class CourseResourceAdmin(object): ‘‘‘课程资源‘‘‘ list_display = [‘course‘, ‘name‘, ‘download‘, ‘add_time‘] search_fields = [‘course‘, ‘name‘, ‘download‘] list_filter = [‘course__name‘, ‘name‘, ‘download‘, ‘add_time‘] # 将管理器与model进行注册关联 xadmin.site.register(Course, CourseAdmin) xadmin.site.register(Lesson, LessonAdmin) xadmin.site.register(Video, VideoAdmin) xadmin.site.register(CourseResource, CourseResourceAdmin)
4、organizations app的models注册
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‘, ‘click_nums‘, ‘fav_nums‘,‘add_time‘ ]
search_fields = [‘name‘, ‘desc‘, ‘click_nums‘, ‘fav_nums‘]
list_filter = [‘name‘, ‘desc‘, ‘click_nums‘, ‘fav_nums‘,‘city__name‘,‘address‘,‘add_time‘]
class TeacherAdmin(object):
‘‘‘老师‘‘‘
list_display = [ ‘name‘,‘org‘, ‘work_years‘, ‘work_company‘,‘add_time‘]
search_fields = [‘org‘, ‘name‘, ‘work_years‘, ‘work_company‘]
list_filter = [‘org__name‘, ‘name‘, ‘work_years‘, ‘work_company‘,‘click_nums‘, ‘fav_nums‘, ‘add_time‘]
xadmin.site.register(CityDict, CityDictAdmin)
xadmin.site.register(CourseOrg, CourseOrgAdmin)
xadmin.site.register(Teacher, TeacherAdmin)
5、operation app的models注册
import xadmin from .models import UserAsk, UserCourse, UserMessage, CourseComments, UserFavorite 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 UserCourseAdmin(object): ‘‘‘用户课程学习‘‘‘ list_display = [‘user‘, ‘course‘, ‘add_time‘] search_fields = [‘user‘, ‘course‘] list_filter = [‘user‘, ‘course‘, ‘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 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‘] # 将后台管理器与models进行关联注册。 xadmin.site.register(UserAsk, UserAskAdmin) xadmin.site.register(UserCourse, UserCourseAdmin) xadmin.site.register(UserMessage, UserMessageAdmin) xadmin.site.register(CourseComments, CourseCommentsAdmin) xadmin.site.register(UserFavorite, UserFavoriteAdmin)
6.xadmin的全局配置
将全局配置修改:
- 如左上角:django Xadmin。下面的我的公司
- 主题修改,app名称汉化,菜单收叠。
使用Xadmin的主题功能。
把全站的配置放在usersadminx.py中:
(1)添加主题功能
from xadmin import views
# 创建xadmin的最基本管理器配置,并与view绑定 class BaseSetting(object): # 开启主题功能 enable_themes = True use_bootswatch = True # 将基本配置管理与view绑定 xadmin.site.register(views.BaseAdminView,BaseSetting)
(2)全局配置
修改django xadmin 和下面的我的公司收起菜单
# 全局修改,固定写法 class GlobalSettings(object): # 修改title site_title = ‘Egon人生无限‘ # 修改footer site_footer = ‘瞎驴的公司‘ # 收起菜单 menu_style = ‘accordion‘ # 将title和footer信息进行注册 xadmin.site.register(views.CommAdminView,GlobalSettings)
(3)修改app的名字
在apps.py里面配置app的显示名称
以users/apps.py为例,其它三个同样操作
默认apps.py里面的代码
from django.apps import AppConfig class UsersConfig(AppConfig): name = ‘users‘
修改后:
from django.apps import AppConfig class UsersConfig(AppConfig): name = ‘users‘ verbose_name = ‘用户‘
还要在users/__init__.py中引用apps.py的配置
添加代码如下:
# users/__init__.py default_app_config = ‘users.apps.UsersConfig‘
设置完成以后,刷新页面如图所示:
以上是关于Django+xadmin打造在线教育平台的主要内容,如果未能解决你的问题,请参考以下文章