安装xadmin
pip install git+git://github.com/sshwsfc/xadmin.git@django2
settings.py里面添加APPS
'xadmin', 'crispy_forms' 'reversion',
urls.py里面添加xadmin后台地址
import xadmin
xadmin.autodiscover()
# version模块自动注册需要版本控制的 Model
from xadmin.plugins import xversion
xversion.register_models()
urlpatterns = patterns('',
path('xadmin/', include(xadmin.site.urls)),
)
迁移模型
python3 makemigrations python3 migrate
如果DEBUG=False,还需要收集静态文件
python3 manage.py collectstatic
现在可以访问/xadmin后台
xadmin导出csv中文乱码解决
将charset=utf-8 改为charset=gbk
xadmin后台注册模型
import xadmin from .models import CourseOrg # 课程直接添加章节 class LessonInline(object): model = Lesson extra = 0 class CourseOrgAdmin(object): # get_zj_nums是模型中定义的方法 list_display = ['name', 'desc', 'click_nums', 'fav_nums', 'get_zj_nums', 'add_time'] search_fields = ['name', 'desc', 'click_nums', 'fav_nums'] # city__name因为city是CourseOrg模型里的外键,City模型中有name字段 list_filter = ['name', 'desc', 'click_nums', 'fav_nums', 'city__name', 'address', 'add_time'] model_icon = 'fa fa-group' # 可以在列表上快速修改内容 list_editable = [ 'degree','desc',] readonly_fields =['click_nums','fav_nums'] exclude = ['fav_nums'] # 下拉框搜索,当有外键指向他时会以ajax方式加载,数据量过大时很有用 relfield_style = 'fk-ajax' # 课程直接添加章节 inlines = [LessonInline] # 后台默认排序 ordering = ['-click_nums'] # 后台可选刷新频率 refresh_times = [3,5] xadmin.site.register(CourseOrg, CourseOrgAdmin) class Course(object): ...... # 自定义函数作为列 def get_zj_nums(self): return self.lesson_set.all().count() get_zj_nums.short_description = "章节数" # 显示自定义的html代码 def go_to(self): from django.utils.safestring import mark_safe # mark_safe禁止转义 return mark_safe("<a href='http://www.baidu.com'>跳转</>") go_to.short_description = "跳转"
Xadmin后台自定义
from xadmin import views class BaseSetting(object): # 开启主题功能 enable_themes = True # 支持切换主题 use_bootswatch = True xadmin.site.register(views.BaseAdminView, BaseSetting)
修改左上角标题、底部footer
from xadmin import views class GlobalSettings(object): site_title = "后台管理系统" site_footer = "Copyright @2016-2017 后台管理系统 All Rights Reserved" # 菜单可折叠 menu_style = "accordion" xadmin.site.register(views.CommAdminView, GlobalSettings)
自定义导航菜单顺序
from xadmin import views class GlobalSetting(object): def get_site_menu(self): return ( {'title': '课程管理', 'menus': ( {'title': '课程信息', 'url': self.get_model_url(Course, 'changelist')}, {'title': '章节信息', 'url': self.get_model_url(Lesson, 'changelist')}, {'title': '视频信息', 'url': self.get_model_url(Video, 'changelist')}, {'title': '课程资源', 'url': self.get_model_url(CourseResource, 'changelist')}, {'title': '课程评论', 'url': self.get_model_url(CourseComments, 'changelist')}, )}, {'title': '机构管理', 'menus': ( {'title': '所在城市', 'url': self.get_model_url(CityDict, 'changelist')}, {'title': '机构讲师', 'url': self.get_model_url(Teacher, 'changelist')}, {'title': '机构信息', 'url': self.get_model_url(CourseOrg, 'changelist')}, )}, {'title': '用户管理', 'menus': ( {'title': '用户信息', 'url': self.get_model_url(UserProfile, 'changelist')}, {'title': '用户验证', 'url': self.get_model_url(EmailVerifyRecord, 'changelist')}, {'title': '用户课程', 'url': self.get_model_url(UserCourse, 'changelist')}, {'title': '用户收藏', 'url': self.get_model_url(UserFavorite, 'changelist')}, {'title': '用户消息', 'url': self.get_model_url(UserMessage, 'changelist')}, )}, {'title': '系统管理', 'menus': ( {'title': '用户咨询', 'url': self.get_model_url(UserAsk, 'changelist')}, {'title': '首页轮播', 'url': self.get_model_url(Banner, 'changelist')}, {'title': '用户分组', 'url': self.get_model_url(Group, 'changelist')}, {'title': '用户权限', 'url': self.get_model_url(Permission, 'changelist')}, {'title': '日志记录', 'url': self.get_model_url(Log, 'changelist')}, )}, xadmin.site.register(views.CommAdminView, GlobalSetting)