插件的集成
本章节根据慕课网上的一个教程集成
DjangoUeditor
富文本编辑器
一、配置步骤
1、直接命令安装(或者直接到
github
上下载源码包)pipenv install DjangoUeditor
2、在
django
的settings.py
中注册DjangoUeditor
的App
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'}