在python3里面使用ueditor(基于adminx)

时间:2024-08-17 13:37:14

第一步,下载ueditor,在git上下载

直接download

https://github.com/twz915/DjangoUeditor3/

然后放到任何一个文件夹里面

配置setting and urls

INSTALLED_APPS = [
'DjangoUeditor',
] #在工程里面新建 media的文件上传目录
MEDIA_URL ='/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#urls里面配置
from extra_apps.DjangoUeditor import  urls as DjangoUeditor_urls urlpatterns = [
    path('ueditor/', include(DjangoUeditor_urls)),
]

然后改app里面的model,

text = UEditorField(verbose_name='文本记录',width=600, height=400,imagePath='user/ueditor/',filePath='user/ueditor/',default='')

然后在adminx.py里面写下调用类型

style_fields = {'text':'ueditor'}

然后在adminx的plugin写ueditor插件

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)

在plugins里面的_init_.py里面配置好 ueditor

PLUGINS = (
'ueditor'
)

ok

如果不出意外,应该已经生效了。可以调用了

https://www.cnblogs.com/tiantianhappy/p/10286528.html