Django-xadmin+ueditor富文本设置
2019-10-08 本文已影响0人
叶扬风起
xadmin文档链接
一、安装
#xadmin
pip install django-crispy-forms
pip install django-reversion
pip install future
pip install httplib2
pip install six
pip install django-formtools
pip install django-import-export
#富文本
pip install DjangoUeditor
因为要再xadmin中添加富文本功能,所以建议再GitHub上现在源码,然后导入项目修改,xadmin地址,将xadmin-django2文件夹里面有个xadmin文件夹复制粘贴到项目目录下的extra_apps目录下,(extra_apps目录设置参考常用设置)
二、配置
INSTALLED_APPS = (
...
#xadmin
'xadmin',
'crispy_forms',
'reversion',
#富文本
'DjangoUeditor',
...
)
三、用于导出Excel所需要的包
pip install xlwt
pip install xlsxwriter
pip install xlrd
四、添加urls
#富文本
path('ueditor/', include('DjangoUeditor.urls' )),
#xadmin后台
path('xadmin/', xadmin.site.urls),
五、在xadmin添加富文本
1. 在xadmin编写ueditor插件
在extra_apps / xadmin ,在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)
2. 将ueditor添加到plugin下的init.py中:
PLUGINS = (
...
'ueditor',
)
3. 将ueditor添加到adminx.py中:
class GoodsAdmin(object):
...
style_fields = {"goods_desc": "ueditor"}