回到顶部

阅读目录

Python3 + Django + xamdin + DjangoUediter,UEditorField 在 xadmin 下加载不出来的解决办法

使用环境

python==3.6.8

django==1.11.18

xadmin

# pip install git+git://github.com/sshwsfc/xadmin.git

DjangoUediter

问题现象

1. UEditorField 在 xadmin 下加载不出来

2. 编辑用户信息,在 xadmin 页面也打开失败

File "E:\yinzhuoqun\djangos\Joyo\extra_apps\xadmin\widgets.py", line 82, in render

'</i></span>%s<span class="input-group-btn"><button class="btn btn-default" type="button">%s</button></span></div></div>' % (input_html[0], _(u'Today'), input_html[1], _(u'Now')))

IndexError: list index out of range

解决办法

path: ~\DjangoBlog\apps_extra\xadmin\plugins 下添加如下文件 ueditor.py:

# -*- coding: utf-8 -*-

__author__ = 'xieboke.net'

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)

添加 'ueditor' 到 PLUGINS

位置: ~\DjangoBlog\apps_extra\xadmin\plugins\__init__.py 里的 的 PLUGINS 里加个  'ueditor',如下

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',
    'importexport',
    'ueditor'
)

修改后重启查看 UEditorField

^_^
请喝咖啡 ×

本作品由 卓越笔记 采用 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 进行许可

前一篇: Sentry 错误监控(Django 错误监控)
下一篇: Notepad++ 实用设置