xadmin集成DjangoUeditor

1、安装

 安装DjangoUeditor

  1)去GitHub上面下载djangoueditor源码包(https://github.com/twz915/DjangoUeditor3)

     然后进入源码的setup.py所在目录,执行pyton setup.py install安装。

  2)直接通过pip命令安装

    pip install DjangoUeditor

 然后将DjangoUeditor放到settings文件的INSTALLED_APPS中

2、配置url

  url(r"^ueditor/", include(DjangoUeditor.urls))

3、在model中使用

  from DjangoUeditor.models import UEditorField

  field = UEditorField(verbose_name = "", width=200, height=300, imagepath="", filepath="", default="", toolbar=[[]])

  常用参数说明:

    verbose_name:字段显示名

    width/height:富文本宽高

    imagepath/filepath:图片/文件存储相对路径(相对settings中配置的MEDIA_ROOT)

    toolbar:工具栏上的功能按钮,注意这里的toolbar是嵌套的列表

4、准备工作都做好了,接下来需要在xadmin中集成调用,集成方法:

  在xadmin--->plugin中新建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):
    # 识别xadmin中的style_field
    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

# 在生成的页面中加入自己的js文件
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)

 

  然后在xadmin--->plugin--->__init__.py文件的PLUGINS中加入我们自定义的插件ueditor

    

  这样就完成了ueditor在xadmin中的集成

  最后,在注册modelXadmin的中,加入style_fields={"字段名":"ueditor"}

 

  

posted @ 2019-01-07 15:50  陌上花开缓缓归!  阅读(595)  评论(0编辑  收藏  举报
Top ↑