Django xadmin集成富文本编辑器
1.下载 DjangoUeditor3 压缩包
https://github.com/twz915/DjangoUeditor3
2.安装
pip install DjangoUeditor3-master.zip
3.配置 settings.py
INSTALLED_APPS = [ ... 'xadmin', 'crispy_forms', 'reversion', 'DjangoUeditor', ]
4.配置 urls.py
path('ueditor/',include('DjangoUeditor.urls' )
5.models.py文件富文本字段
content=UEditorField(verbose_name='内容', width=700, height=400, toolbars='full', imagePath='ueditor/images/', filePath='ueditor/files/', upload_settings={'imageMaxSizing':1024000},
6.在xadmin下的plugin中新建一个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)
7.将ueditor插件添加到plugins中的__init__.py的PLUGINS中
'multiselect', 'themes', 'aggregation', # 'mobile', 'passwords', 'sitemenu', 'language', 'quickfilter', 'sortablelist', 'importexport', 'ueditor'
8.adminx.py文件,配置插件
style_fields = {'content':'ueditor'}