Django中在xadmin中集成DjangoUeditor过程详解

  

<强>环境

  

python版本:3.6

  

django: 1.10.8   

<强> 1。下载xadmin

  https://github.com/sshwsfc/xadmin

  

下载DjangoUeditor   https://github.com/twz915/DjangoUeditor3

  

<强> 2。直接将xadmin和DjangoUeditor集成在pycharm里,在项目下新建一个文件夹extra_apps,将与xadmin, DjangoUeditor的同名文件复制在extra_apps下

  

 Django中在xadmin中集成DjangoUeditor过程详解

  

<强> 3。在settings.py里注册DjangoUeditor

        INSTALLED_APPS=[   …   # xadmin第三方插件,实现富文本编辑   “DjangoUeditor”   )      

<>强4。在url里对其进行配置

        url (r ' ^ ueditor/',包括(DjangoUeditor.urls))      

<强> 5。在xadmin中添加插件ueditor

  

 Django中在xadmin中集成DjangoUeditor过程详解

  

在xadmin——》插件下新建ueditor.py

        进口xadmin   从xadmin。视图导入BaseAdminPlugin、CreateAdminView ModelFormAdminView UpdateAdminView   从DjangoUeditor。模型导入UEditorField   从DjangoUeditor。小部件进口UEditorWidget   django。配置导入设置         类XadminUEditorWidget (UEditorWidget):   def __init__(自我,* * kwargs):   self.ueditor_options=kwargs   self.Media。js=没有   超级(XadminUEditorWidget,自我). __init__ (kwargs)         类UeditorPlugin (BaseAdminPlugin):      def get_field_style(自我、attrs db_field、风格、* * kwargs):   如果风格==皍editor”:   如果isinstance (db_field UEditorField):   .widget部件=db_field.formfield ()   param={}   param.update (widget.ueditor_settings)   param.update (widget.attrs)   返回{“部件”:XadminUEditorWidget (* * param)}   返回attrs      def block_extrahead(自我、上下文节点):   js=' & lt;脚本type=" text/javascript " src=" https://www.yisu.com/zixun/%s "祝辞& lt;/script>“%(设置。STATIC_URL +“ueditor/ueditor.config.js”) #自己的静态目录   js +=& lt;脚本type=" text/javascript " src=" https://www.yisu.com/zixun/%s "祝辞& lt;/script>“%(设置。STATIC_URL +“ueditor/ueditor.all.min.js”) #自己的静态目录   nodes.append (js)      xadmin.site。register_plugin (UeditorPlugin UpdateAdminView)   xadmin.site。register_plugin (UeditorPlugin CreateAdminView)      

<强> 6。在xadmin -》插件》__init__ . py中添加ueditor

        插件=(   “行动”,   “过滤器”,   “书签”,   “出口”,   “布局”,   “刷新”,   “详细信息”,   “可编辑”,   “联系”,   “图”,   “ajax”,   “relfield”,   “内联”,   “topnav”,   “门户”,   “quickform”,   “向导”,   “图像”,   “身份验证”,   “多选”,   “主题”,   “聚合”,   “移动”,   “密码”,   “sitemenu”,   “语言”,   “quickfilter”,   “sortablelist”,   “进出口”   “ueditor”   )      

<强> 7。将ueditor添加到adminx.py中

  

这里的NoticeContent是指使用UEditorField的字段

        类NoticeAdmin(对象):按照list_display=[‘NoticeTitle’,‘NoticeContent’,‘NoticeDesc’,‘NoticeCategory’,‘NoticeData’,‘NoticeUser] style_fields={“NoticeContent”:“ueditor”}      

<强> 8。运行结果:

  

 Django中在xadmin中集成DjangoUeditor过程详解

  

9。前端显示需要加上:

        {% autoescape了%}   {% endautoescape %}      

注意:要想在富文本编辑框中显示出图片,必须在settings.py里设置路径:

        MEDIA_ROOT=os.path。加入(BASE_DIR,“媒体”)。替换(“\ \”,“/?#设置静态文件路径为主目录下的媒体文件夹   MEDIA_URL='/媒体/'      

在与项目同名的文件下的urls . py中添加:

        urlpattern=[   url (admin/, admin.site.urls),   url (r ' ^ ueditor/',包括(DjangoUeditor.urls)),   )+静态(设置。MEDIA_URL document_root=settings.MEDIA_ROOT)

Django中在xadmin中集成DjangoUeditor过程详解