插件的集成

本章节根据慕课网上的一个教程集成DjangoUeditor富文本编辑器

一、配置步骤

  • 1、直接命令安装(或者直接到github上下载源码包)

    pipenv install DjangoUeditor
    
  • 2、在djangosettings.py中注册DjangoUeditorApp

  • 3、在根urls.py中配置路由

    # 富文本相关url
    path('ueditor/',include('DjangoUeditor.urls' )),
    
  • 4、在models.py中修改定义的字段

    # detail = models.TextField(verbose_name='课程详情')
    detail = UEditorField(verbose_name=u'课程详情', width=600, height=300, imagePath="courses/ueditor/", filePath="courses/ueditor/", default='')
    

二、在xadmin中集成

  • 1、在xadmin的源码包中的plugins中定义一个插件名ueditor.py

    import xadmin
    from xadmin.views import BaseAdminPlugin, CreateAdminView, 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)
    
  • 2、在adminx.py中使用ueditor样式

    class CourseModelConfig(object):
        list_display = ['name', 'desc', 'detail', 'click_nums', 'create_at', 'update_at', 'get_zj_nums', 'go_to']
        ...
        style_fields = {'detail': 'ueditor'}
    

results matching ""

    No results matching ""