源码获取

项目已集成,直接复制里面的 DjangoUeditor
https://github.com/ALVIN-YANG/article
或在 https://github.com/twz915/DjangoUeditor3.git 下载源码集成

把 DjangoUeditor 放在项目 source root 目录,也就是和其他 app 同级

添加到 INSTALLED_APPS

  1. INSTALLED_APPS = (
  2. 'DjangoUeditor',
  3. )

配置 media 路径

  1. MEDIA_URL = "/media/"
  2. MEDIA_ROOT = os.path.join(BASE_DIR, "media").replace('\\','/')

配置 urls

  1. from django.urls import path, include, re_path
  2. import xadmin
  3. from .settings import MEDIA_ROOT
  4. from django.views.static import serve
  5. urlpatterns = [
  6. path('admin/', xadmin.site.urls),
  7. path('ueditor/', include('DjangoUeditor.urls')),
  8. re_path('^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT}),
  9. ]

添加到 xadmin 的 plugins

在 xadmin 的 plugins 中的 init.py文件添加

  1. PLUGINS = (
  2. ...
  3. 'ueditor',
  4. ...
  5. )

在 xadmin 的 plugins 中新建 ueditor.py 文件 输入:

  1. import xadmin
  2. from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView
  3. from DjangoUeditor.models import UEditorField
  4. from DjangoUeditor.widgets import UEditorWidget
  5. from django.conf import settings
  6. class XadminUEditorWidget(UEditorWidget):
  7. def __init__(self,**kwargs):
  8. self.ueditor_options=kwargs
  9. self.Media.js = None
  10. super(XadminUEditorWidget,self).__init__(kwargs)
  11. class UeditorPlugin(BaseAdminPlugin):
  12. def get_field_style(self, attrs, db_field, style, **kwargs):
  13. if style == 'ueditor':
  14. if isinstance(db_field, UEditorField):
  15. widget = db_field.formfield().widget
  16. param = {}
  17. param.update(widget.ueditor_settings)
  18. param.update(widget.attrs)
  19. return {'widget': XadminUEditorWidget(**param)}
  20. return attrs
  21. def block_extrahead(self, context, nodes):
  22. js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js") #自己的静态目录
  23. js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js") #自己的静态目录
  24. nodes.append(js)
  25. xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)
  26. xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)

models 中

  1. from django.db import models
  2. from DjangoUeditor.models import UEditorField
  3. class Article(models.Model):
  4. title = models.CharField('标题', max_length=50)
  5. content = UEditorField(u'内容', width=600, height=300, toolbars="full", imagePath="media/", filePath="media/",
  6. upload_settings={"imageMaxSize": 1204000}, blank=True)
  7. class Meta:
  8. verbose_name = '文章'
  9. verbose_name_plural = verbose_name
  10. def __str__(self):
  11. return self.title

adminx.py 配置

添加 style_fields = {‘content’: ‘ueditor’} 配置

  1. import xadmin
  2. from xadmin.views import ModelAdminView
  3. from xadmin.filters import DateFieldListFilter
  4. from .models import Article
  5. class ArticleModelAdmin(ModelAdminView):
  6. list_display = 'title',
  7. style_fields = {'content': 'ueditor'}
  8. xadmin.site.register(Article, ArticleModelAdmin)