目錄javascript
代碼python
github下載git
教程github
學習自慕課網-使用python3.x與Django2.0.1開發的在線教育平臺django
(1)用戶權限
超級用戶擁有全部權限,其它添加的用戶默認沒有任何權限
進後臺添加一個用戶「Editor1」,勾上「職員狀態」後,這個用戶才能夠登陸進後臺,默認沒添加權限的用戶登陸到後臺的狀況以下:
接下來,爲用戶Editor1添加查看課程和查看章節的權限
再刷新能夠看到,有了查看課程和章節的權限了
(2)組的權限
添加一個組「編輯部門」,賦予以下權限 :
把剛纔的用戶「Editor1」加入到「編輯部門」這個組,而後看用戶如今的權限以下:
組裏面的成員不但擁有本身自己的權限外,還會擁有組的權限
xadmin的圖標採用的是第三方css樣式「font awesome」,咱們能夠進官網下載最新的樣式替代本來的,下載地址:http://www.fontawesome.com.cn/
下載完後把裏面的「css」和「fonts」兩個文件夾拷貝到xadmin的源碼(路徑:xadmin/static/vendor/font-awesome)裏面
使用方法:
以course爲例
(1)進官網找到圖標的樣式
(2)course/adminx.py使用
# 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'] model_icon = 'fa fa-book'
再在後臺刷新(ctrl+F5),就能夠看到圖標了
課程:
# 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'] #過濾 model_icon = 'fa fa-book' #圖標 ordering = ['-click_nums'] #排序 readonly_fields = ['click_nums'] #只讀字段,不能編輯 exclude = ['fav_nums'] #不顯示的字段
目前在添加課程的時候無法添加章節和課程資源,咱們能夠用inlines去實現這一功能
class LessonInline(object): model = Lesson extra = 0 class CourseResourceInline(object): model = CourseResource extra = 0 #在CourseAdmin中使用inlines添加上面兩個的方法 class CourseAdmin(object): inlines = [LessonInline,CourseResourceInline] #增長章節和課程資源
效果以下:
再添加課程的時候,能夠直接添加章節和課程資源
課程裏面分爲輪播課程和不是輪播課程兩種類型,咱們能夠分開來管理
(1)在course/models.py裏面新建一個Model
class BannerCourse(Course): '''顯示輪播課程''' class Meta: verbose_name = '輪播課程' verbose_name_plural = verbose_name #這裏必須設置proxy=True,這樣就不會再生成一張表,同時還具備Model的功能 proxy = True
(2)course/adminx.py
from .models import BannerCourse 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'] #過濾 model_icon = 'fa fa-book' #圖標 ordering = ['-click_nums'] #排序 readonly_fields = ['click_nums'] #只讀字段 exclude = ['fav_nums'] #不顯示的字段 inlines = [LessonInline,CourseResourceInline] #增長章節和課程資源 def queryset(self): # 重載queryset方法,來過濾出咱們想要的數據的 qs = super(CourseAdmin, self).queryset() # 只顯示is_banner=True的課程 qs = qs.filter(is_banner=False) return qs class BannerCourseAdmin(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'] model_icon = 'fa fa-book' ordering = ['-click_nums'] readonly_fields = ['click_nums'] exclude = ['fav_nums'] inlines = [LessonInline,CourseResourceInline] def queryset(self): #重載queryset方法,來過濾出咱們想要的數據的 qs = super(BannerCourseAdmin, self).queryset() #只顯示is_banner=True的課程 qs = qs.filter(is_banner=True) return qs # 將管理器與model進行註冊關聯 xadmin.site.register(Course, CourseAdmin) xadmin.site.register(BannerCourse, BannerCourseAdmin)
後臺:能夠看到多了一個輪播課程,來達到分類管理的一個功能
(1)list_editable
在列表頁能夠直接編輯的
class CourseAdmin(object): list_editable = ['degree','desc']
(2)自定義函數做爲列顯示
course/models.py中
class Course(models.Model): ' ' ' def get_zj_nums(self): #獲取課程的章節數 return self.lesson_set.all().count() get_zj_nums.short_description = '章節數' #在後臺顯示的名稱
course/adminx.py中
class CourseAdmin(object): list_display = ['get_zj_nums'] #直接使用函數名做爲字段顯示
效果:列表字段多了個「章節數」
(3)顯示自定義的html代碼
course/models.py中
class Course(models.Model): . . . def go_to(self): from django.utils.safestring import mark_safe #mark_safe後就不會轉義 return mark_safe("<a href='https://home.cnblogs.com/u/derek1184405959/'>跳轉</a>") go_to.short_description = "跳轉"
course/adminx.py中
class CourseAdmin(object): list_display = ['go_to']
效果:多了一個列表「跳轉」,點擊後跳轉到上面定義的地址
(4)refresh定時刷新工具
course/adminx.py中
class CourseAdmin(object): refresh_times = [3,5] #自動刷新(裏面是秒數)
後臺效果:
能夠選擇3s或者5s自動刷新頁面
(5)字段聯動
應用場景:當添加一門課程的時候,但願課程機構裏面的課程數 +1
重寫xadmin的save_models方法
class CourseAdmin(object): . . . def save_models(self): # 在保存課程的時候統計課程機構的課程數 # obj實際是一個course對象 obj = self.new_obj # 若是這裏不保存,新增課程,統計的課程數會少一個 obj.save() # 肯定課程的課程機構存在。 if obj.course_org is not None: #找到添加的課程的課程機構 course_org = obj.course_org #課程機構的課程數量等於添加課程後的數量 course_org.course_nums = Course.objects.filter(course_org=course_org).count() course_org.save()
# course/adminx.py import xadmin from .models import Course, Lesson, Video, CourseResource,BannerCourse from organization.models import CourseOrg class LessonInline(object): model = Lesson extra = 0 class CourseResourceInline(object): model = CourseResource extra = 0 # Course的admin管理器 class CourseAdmin(object): '''課程''' list_display = [ 'name','desc','detail','degree','learn_times','students','get_zj_nums','go_to'] #顯示的字段 search_fields = ['name', 'desc', 'detail', 'degree', 'students'] #搜索 list_filter = [ 'name','desc','detail','degree','learn_times','students'] #過濾 model_icon = 'fa fa-book' #圖標 ordering = ['-click_nums'] #排序 readonly_fields = ['click_nums'] #只讀字段 exclude = ['fav_nums'] #不顯示的字段 list_editable = ['degree','desc'] # refresh_times = [3,5] #自動刷新(裏面是秒數範圍) inlines = [LessonInline,CourseResourceInline] #增長章節和課程資源 def queryset(self): # 重載queryset方法,來過濾出咱們想要的數據的 qs = super(CourseAdmin, self).queryset() # 只顯示is_banner=True的課程 qs = qs.filter(is_banner=False) return qs def save_models(self): # 在保存課程的時候統計課程機構的課程數 # obj實際是一個course對象 obj = self.new_obj # 若是這裏不保存,新增課程,統計的課程數會少一個 obj.save() # 肯定課程的課程機構存在。 if obj.course_org is not None: #找到添加的課程的課程機構 course_org = obj.course_org #課程機構的課程數量等於添加課程後的數量 course_org.course_nums = Course.objects.filter(course_org=course_org).count() course_org.save() class BannerCourseAdmin(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'] model_icon = 'fa fa-book' ordering = ['-click_nums'] readonly_fields = ['click_nums'] exclude = ['fav_nums'] inlines = [LessonInline,CourseResourceInline] def queryset(self): #重載queryset方法,來過濾出咱們想要的數據的 qs = super(BannerCourseAdmin, self).queryset() #只顯示is_banner=True的課程 qs = qs.filter(is_banner=True) return qs 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(BannerCourse, BannerCourseAdmin) xadmin.site.register(Lesson, LessonAdmin) xadmin.site.register(Video, VideoAdmin) xadmin.site.register(CourseResource, CourseResourceAdmin)
(1)下載
地址:https://github.com/twz915/DjangoUeditor3/
解壓後,把DjangoUeditor文件夾拷貝到項目目錄下面
注意:直接pip install DjangoUeditor的方法會出問題
(2)settings中添加app
INSTALLED_APPS = [ 'DjangoUeditor', ]
(3)MxOnline/urls.py
# 富文本編輯器url path('ueditor/',include('DjangoUeditor.urls' )),
(4)course/models.py中Course修改detail字段
class Course(models.Model): # detail = models.TextField("課程詳情") detail = UEditorField(verbose_name=u'課程詳情', width=600, height=300, imagePath="courses/ueditor/", filePath="courses/ueditor/", default='')
(5)xadmin/plugs目錄下新建ueditor.py文件,代碼以下
import xadmin from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView from DjangoUeditor.models import UEditorField from DjangoUeditor.widgets import UEditorWidget from django.conf import settings class XadminUEditorWidget(UEditorWidget): def __init__(self, **kwargs): self.ueditor_options = kwargs self.Media.js = None super(XadminUEditorWidget,self).__init__(kwargs) class UeditorPlugin(BaseAdminPlugin): def get_field_style(self, attrs, db_field, style, **kwargs): if style == 'ueditor': if isinstance(db_field, UEditorField): widget = db_field.formfield().widget param = {} param.update(widget.ueditor_settings) param.update(widget.attrs) return {'widget':XadminUEditorWidget(**param)} return attrs def block_extrahead(self, context, nodes): js = '<script type="text/javascript" src="%s"></script>' %(settings.STATIC_URL + "ueditor/ueditor.config.js") js += '<script type="text/javascript" src="%s"></script>' %(settings.STATIC_URL + "ueditor/ueditor.all.min.js") nodes.append(js) xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView) xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)
(6)xadmin/plugs/__init__.py裏面添加ueditor插件
PLUGINS = ( 'ueditor', )
(7)course/adminx.py中使用
class CourseAdmin(object): #detail就是要顯示爲富文本的字段名 style_fields = {"detail": "ueditor"}
(8)course-detail.html
在模板中必須關閉Django的自動轉義才能正常顯示
<div class="tab_cont tab_cont1"> {% autoescape off %} {{ course.detail }} {% endautoescape %} </div>
最終效果:
後臺編輯頁面
前端顯示