源码获取
项目已集成,直接复制里面的 DjangoUeditor
https://github.com/ALVIN-YANG/article
或在 https://github.com/twz915/DjangoUeditor3.git 下载源码集成
把 DjangoUeditor 放在项目 source root 目录,也就是和其他 app 同级
添加到 INSTALLED_APPS
INSTALLED_APPS = (
'DjangoUeditor',
)
配置 media 路径
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media").replace('\\','/')
配置 urls
from django.urls import path, include, re_path
import xadmin
from .settings import MEDIA_ROOT
from django.views.static import serve
urlpatterns = [
path('admin/', xadmin.site.urls),
path('ueditor/', include('DjangoUeditor.urls')),
re_path('^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),
]
添加到 xadmin 的 plugins
在 xadmin 的 plugins 中的 init.py文件添加
PLUGINS = (
...
'ueditor',
...
)
在 xadmin 的 plugins 中新建 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)
models 中
from django.db import models
from DjangoUeditor.models import UEditorField
class Article(models.Model):
title = models.CharField('标题', max_length=50)
content = UEditorField(u'内容', width=600, height=300, toolbars="full", imagePath="media/", filePath="media/",
upload_settings={"imageMaxSize": 1204000}, blank=True)
class Meta:
verbose_name = '文章'
verbose_name_plural = verbose_name
def __str__(self):
return self.title
adminx.py 配置
添加 style_fields = {‘content’: ‘ueditor’} 配置
import xadmin
from xadmin.views import ModelAdminView
from xadmin.filters import DateFieldListFilter
from .models import Article
class ArticleModelAdmin(ModelAdminView):
list_display = 'title',
style_fields = {'content': 'ueditor'}
xadmin.site.register(Article, ArticleModelAdmin)