django+xadmin在线教育平台慕学网(二)
三、xadmin后台管理
3.1 、xadmin的安装
github下在xadmin源码:下载地址
下载完成之后,解压复制xadmin文件夹
3.2、xadmin配置
(1)新建python package “extra_apps”,把xadmin文件夹复制到extra_apps文件夹下面
(2)把extra_apps右键mark为Source Root
(3)在settings中加入
sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))
(4)配置路由
把admin改成xadmin
#mxonline/urls.py from django.conf.urls import url import xadmin urlpatterns = [ url(r'^xdmin/', xadmin.site.urls), ]
(5)注册app
把下面两个app注册到settings.py的INSTALLED_APPS中
'xadmin', 'crispy_forms',
(6)重新生成数据库
python manage.py makemigrations
python manage.py migrate
(7)将xadmin后台设置成中文街面
LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = False
(8)创建超级用户(管理员用户)
python manage.py createsuperuser
根据提示输入账号名、邮箱、密码
(9)运行,登录
python manage.py runserver
访问:http://127.0.0.1:8000/xadmin/
3.3、models注册
- users下创建adminx.py
import xadmin from .models import EmailVerifyRecord, Banner # 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'] 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)
- course下创建adminx.py
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)
- organization下创建adminx.py
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)
- operation下创建adminx.py
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)
完成之后,启动项目,后台管理界面如下:
3.4、xadmin的全局配置
将全局配置修改:
- 如左上角:django Xadmin。下面的我的公司
- 主题修改,app名称汉化,菜单收叠。
使用xadmin的主题功能
把全站的配置放在users/adminx.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 admin和下面的我的公司
# 全局修改,固定写法 class GlobalSettings(object): # 修改title site_title = 'xxxxxx' # 修改footer site_footer = 'xoxoxo' # 收起菜单 menu_style = 'accordion' # 将title和footer信息进行注册 xadmin.site.register(views.CommAdminView,GlobalSettings)
(3)修改在xadmin后台系统app的名字
在每个app下面的apps.py文件
#users/apps.py from django.apps import AppConfig class UsersConfig(AppConfig): name = 'users' verbose_name = '用户'
在每个app下面__init__.py中引用apps.py的配置
#users/__init__.py default_app_config = 'users.apps.UsersConfig'
#operation/apps.py from django.apps import AppConfig class OperationConfig(AppConfig): name = 'operation' verbose_name = '用户操作' #operation/__init__.py
default_app_config = 'operation.apps.OperationConfig'
#course/apps.py from django.apps import AppConfig class CourseConfig(AppConfig): name = 'course' verbose_name = '课程' #course/__init.py__ default_app_config = 'course.apps.CourseConfig'
#organization/apps.py from django.apps import AppConfig class OrganizationConfig(AppConfig): name = 'organization' verbose_name = '机构' #organization/__init__.py default_app_config = 'organization.apps.OrganizationConfig'