Django xadmin引入DjangoUeditor
Django xadmin引入DjangoUeditor
版本:python3.6.1,Django1.11.1
DjangoUeditor下载地址:https://github.com/twz915/DjangoUeditor3
1、放入自定义第三方目录,settings.py配置
import os import sys sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps')) INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'DjangoUeditor', ]
2、urls.py配置
url(r'^ueditor/', include('DjangoUeditor.urls')),
3、models.py配置
detail = UEditorField(verbose_name='详情内容', width=1000, height=300,imagePath="uploads/images/",filePath="uploads/files/", default='', blank=True)
xadmin插件引入
1、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、xadmin/plugin目录下__init__.py
PLUGINS = ( 'actions', 'filters', 'bookmark', 'export', 'layout', 'refresh', 'details', 'editable', 'relate', 'chart', 'ajax', 'relfield', 'inline', 'topnav', 'portal', 'quickform', 'wizard', 'images', 'auth', 'multiselect', 'themes', 'aggregation', 'mobile', 'passwords', 'sitemenu', 'language', 'quickfilter', 'sortablelist', 'execl', 'ueditor', )
3、xadmin.py引入
style_fields = {'detail': 'ueditor'}
4、上线后将ueditor静态文件放入STATIC_ROOT
python manage.py collectstatic