admin是基于Django开发的后台管理框架,方便,快捷,而且简单;
而xadmin就相当于admin的升级版,更加强大。
1、安装xadmin(源码安装方式) 教程
PS:
- 卸载pip安装的xadminpip uninstall xadmin
- 过教程途中,看自身情况可能还需要自行下载其他的依赖库,如下:
pip install django-formtools
pip install httplib2
pip install django-crispy-forms
不得不说,这一步还是有点麻烦的,因为我竟然又在这里卡了一个下午。。。。救命
成功之后,你是可以看到这么一个后台管理界面的:
2、users下的models注册(admin.py):
# Register your models here.
import xadminfrom .models import EmailVerifyRecord
from .models import 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']xadmin.site.register(EmailVerifyRecord, EmailVerifyRecordAdmin)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、其他三个App的model注册:
# course/adminx.pyimport xadminfrom .models import Course, Lesson, Video, CourseResource# Course的admin管理器
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.pyimport xadminfrom .models import CityDict, CourseOrg, Teacherclass 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.pyimport xadminfrom .models import UserAsk, UserCourse, UserMessage, CourseComments, UserFavoriteclass 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)
运行后进后台,如下图:
美滋滋,又到中秋节了!!!各位节日快乐啊~~~
溜了溜了~~~啦啦啦啦
未完待续~~~