码迷,mamicode.com
首页 > 其他好文 > 详细

(项目)在线教育平台(二)

时间:2018-11-15 14:32:47      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:运行   搜索   pattern   字段   pip   this   glob   增删改查   生成   

四、xadmin后台管理系统

1、xadmin安装

  首先在该网址下载xadmin的源码包:https://github.com/sshwsfc/xadmin/tree/django2

  下载完成之后,将该安装包放在项目目录下进行安装:pip install xadmin-django2.zip

  这时它会安装许多xadmin相关的依赖包,然后在项目根目录下新建包extra_apps,将下载好的xadmin源码解压,将里面的xadmin文件拷贝到该包下,我们是用源码安装的方式,需要将xadmin卸载:pip uninstall xadmin

技术分享图片

  我们使用的是源码的方式,需要将xadmin和crispy_forms配置进setting.py中的INSTALLED_APPS中:

 1 INSTALLED_APPS = [
 2     django.contrib.auth,
 3     django.contrib.contenttypes,
 4     django.contrib.sessions,
 5     django.contrib.messages,
 6     django.contrib.staticfiles,
 7     django.contrib.admin,
 8     users,
 9     course,
10     organization,
11     operation,
12     xadmin,
13     crispy_forms
14 ]

  将extra_apps包Mark成Source Root,并在settings.py中进行配置:

1 import os
2 import sys
3 
4 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
6 sys.path.insert(0, os.path.join(BASE_DIR, apps))
7 sys.path.insert(0, os.path.join(BASE_DIR, extra_apps))

  然后配置xadmin的路由,在MxOnline/urls.py文件中配置路由:

1 import xadmin
2 
3 urlpatterns = [
4     path(xadmin/, xadmin.site.urls),
5 ]

  然后就可以进行迁移数据库,生成xadmin后台管理所需的表,注意,如果提示RuntimeError,需要在INSTALLED_APPS中配置django.contrib.admin。

  现在就可以使用xadmin后台管理系统了,不过要先创建一个管理员用户,打开Run manage.py Task,运行createsuperuser创建管理员用户:

技术分享图片

  创建好之后就可以登录后台管理系统了:http://127.0.0.1:8000/xadmin/

技术分享图片

  右侧都是英文,我们需要设置成中文显示,在settings.py文件中进行配置:

1 LANGUAGE_CODE = zh-hans
2 
3 TIME_ZONE = Asia/Shanghai
4 
5 USE_I18N = True
6 
7 USE_L10N = True
8 
9 USE_TZ = False

2、models注册

  现在需要将我们设计好的每个models中的表注册进后台管理系统中,以实现数据的增删改查。

2.1 user的model注册

  在users下面创建adminx.py文件,在该文件下编写注册代码:

 1 import xadmin
 2 
 3 from users.models import EmailVerifyRecord, Banner
 4 
 5 
 6 class EmailVerifyRecordAdmin(object):
 7     # 显示的列
 8     list_display = [code, email, send_type, send_time]
 9     # 搜索的字段
10     search_fields = [code, email, send_type]
11     # 过滤
12     list_filter = [code, email, send_type, send_time]
13 
14 xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)
15 
16 
17 class BannerAdmin(object):
18     list_display = [title, image, url,index, add_time]
19     search_fields = [title, image, url,index]
20     list_filter = [title, image, url,index, add_time]
21 
22 xadmin.site.register(Banner, BannerAdmin)

  刷新页面之后,左侧导航栏出现了邮箱验证码和轮播图的选项,还有自己设置的显示列表选项,筛选选项,搜索选项,界面显示如下: 技术分享图片

2.2 course的model注册

  在course下面创建adminx.py文件,在该文件下编写注册代码:

 1 import xadmin
 2 
 3 from .models import Course, Lesson, Video, CourseResourse
 4 
 5 class CourseAdmin(object):
 6     list_display = [name,desc,detail,degree,learn_times,students]
 7     search_fields = [name, desc, detail, degree, students]
 8     list_filter = [name,desc,detail,degree,learn_times,students]
 9 
10 xadmin.site.register(Course, CourseAdmin)
11 
12 
13 class LessonAdmin(object):
14     list_display = [course, name, add_time]
15     search_fields = [course, name]
16     # course__name是根据课程名称进行过滤
17     list_filter = [course__name, name, add_time]
18 
19 xadmin.site.register(Lesson, LessonAdmin)
20 
21 
22 class VideoAdmin(object):
23     list_display = [lesson, name, add_time]
24     search_fields = [lesson, name]
25     list_filter = [lesson, name, add_time]
26 
27 xadmin.site.register(Video, VideoAdmin)
28 
29 
30 class CourseResourseAdmin(object):
31     list_display = [course, name, download, add_time]
32     search_fields = [course, name, download]
33     list_filter = [course__name, name, download, add_time]
34 
35 xadmin.site.register(CourseResourse, CourseResourseAdmin)

2.3 organization的model注册

  在organization下面创建adminx.py文件,在该文件下编写注册代码:

 1 import xadmin
 2 
 3 from .models import CityDict, CourseOrg, Teacher
 4 
 5 
 6 class CityDictAdmin(object):
 7     list_display = [name, desc, add_time]
 8     search_fields = [name, desc]
 9     list_filter = [name, desc, add_time]
10 
11 xadmin.site.register(CityDict, CityDictAdmin)
12 
13 
14 class CourseOrgAdmin(object):
15     list_display = [name, desc, click_nums, fav_nums,add_time]
16     search_fields = [name, desc, click_nums, fav_nums]
17     list_filter = [name, desc, click_nums, fav_nums,city__name,address,add_time]
18 
19 xadmin.site.register(CourseOrg, CourseOrgAdmin)
20 
21 
22 class TeacherAdmin(object):
23     list_display = [name,org, work_years, work_company,add_time]
24     search_fields = [org, name, work_years, work_company]
25     list_filter = [org__name, name, work_years, work_company,click_nums, fav_nums, add_time]
26 
27 xadmin.site.register(Teacher, TeacherAdmin)

2.4 operation的model注册

  在operation下面创建adminx.py文件,在该文件下编写注册代码:

 1 import xadmin
 2 
 3 from .models import UserAsk, UserMessage, CourseComments, UserCourse, UserFavorite
 4 
 5 
 6 class UserAskAdmin(object):
 7     list_display = [name, mobile, course_name, add_time]
 8     search_fields = [name, mobile, course_name]
 9     list_filter = [name, mobile, course_name, add_time]
10 
11 xadmin.site.register(UserAsk, UserAskAdmin)
12 
13 
14 class UserMessageAdmin(object):
15     list_display = [user, message, has_read, add_time]
16     search_fields = [user, message, has_read]
17     list_filter = [user, message, has_read, add_time]
18 
19 xadmin.site.register(UserMessage, UserMessageAdmin)
20 
21 
22 class CourseCommentsAdmin(object):
23     list_display = [user, course, comments, add_time]
24     search_fields = [user, course, comments]
25     list_filter = [user, course, comments, add_time]
26 
27 xadmin.site.register(CourseComments, CourseCommentsAdmin)
28 
29 
30 class UserCourseAdmin(object):
31     list_display = [user, course, add_time]
32     search_fields = [user, course]
33     list_filter = [user, course, add_time]
34 
35 xadmin.site.register(UserCourse, UserCourseAdmin)
36 
37 
38 class UserFavoriteAdmin(object):
39     list_display = [user, fav_id, fav_type, add_time]
40     search_fields = [user, fav_id, fav_type]
41     list_filter = [user, fav_id, fav_type, add_time]
42 
43 xadmin.site.register(UserFavorite, UserFavoriteAdmin)

  至此,后台管理中所有的model注册完毕,界面显示如下:

技术分享图片

3、xadmin的全局配置

  修改左上角的显示标题,还有最底部的我的公司,主题功能,app名称汉化、左侧菜单收叠,这些修改的配置都写进users/adminx.py文件中:

3.1 主题功能

1 from xadmin import views
2 
3 class BaseSetting(object):
4     """主题配置"""
5     enable_themes = True
6     use_bootswatch = True
7 
8 xadmin.site.register(views.BaseAdminView, BaseSetting)

  配置之后,后台管理界面的右上角出现了主题,可以进行主题更换:

技术分享图片

3.2 logo、底部我的公司,菜单收叠

 1 class GlobalSetting(object):
 2     """ logo、底部我的公司,菜单收叠配置"""
 3     # logo
 4     site_title = 知能网在线教育后台管理
 5     # 我的公司
 6     site_footer = 知能网
 7     # 菜单收叠
 8     menu_style = accordion
 9 
10 xadmin.site.register(views.CommAdminView, GlobalSetting)

  刷新页面之后,修改的地方如下:

技术分享图片

3.3 app名称汉化

  修改每个模块中的app.py文件,以users/apps.py为例,在文件中添加verbose_name = ‘用户‘,修改内容如下:

1 from django.apps import AppConfig
2 
3 
4 class UsersConfig(AppConfig):
5     name = users
6     verbose_name = 用户管理

  然后在__init__.py文件中引用app.py文件中的配置:

1 default_app_config = users.apps.UsersConfig

  然后依次修改其他模块中的内容,最后修改完,界面显示如下:

技术分享图片

 

(项目)在线教育平台(二)

标签:运行   搜索   pattern   字段   pip   this   glob   增删改查   生成   

原文地址:https://www.cnblogs.com/Sweltering/p/9951259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!