千家信息网

django xadmin如何集成DjangoUeditor富文本编辑器

发表于:2025-11-06 作者:千家信息网编辑
千家信息网最后更新 2025年11月06日,这篇文章将为大家详细讲解有关django xadmin如何集成DjangoUeditor富文本编辑器,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。介绍Ueditor
千家信息网最后更新 2025年11月06日django xadmin如何集成DjangoUeditor富文本编辑器

这篇文章将为大家详细讲解有关django xadmin如何集成DjangoUeditor富文本编辑器,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

介绍
  • Ueditor HTML编辑器是百度开源的在线HTML编辑器,功能非常强大

    额外功能
  • 解决图片视频等无法上传显示问题

Ueditor下载地址 https://github.com/wsqy/DjangoUeditor.git
  • 解压后将 DjangoUeditor 文件夹复制到django项目目录下,跟app目录同级

修改app models
  • 导入UEditorField 模块

  • 增加需要富文本框的字段

from DjangoUeditor.models import UEditorFieldclass Post(models.Model):    """文章"""    STATUS_CHOICES = (        ('draft','草稿'),        ('published','已发布'),        )    ...    body = UEditorField('内容', height=300, width=800,max_length=1024000000000,                           default=u'', blank=True, imagePath="images/",                           toolbars='besttome', filePath='files/')    ...
  • 说明:
    UEditorField继承自models.TextField,因此你可以直接将model里面定义的models.TextField直接改成UEditorField即可。

    UEditorField提供了额外的参数:

    toolbars:配置你想显示的工具栏,取值为mini,normal,full,besttome, 代表小,一般,全部,涂伟忠贡献的一种样式。如果默认的工具栏不符合您的要求,您可以在settings里面配置自己的显示按钮。参见后面介绍。

    imagePath:图片上传的路径,如"images/",实现上传到"{{MEDIA_ROOT}}/images"文件夹

    filePath:附件上传的路径,如"files/",实现上传到"{{MEDIA_ROOT}}/files"文件夹

    scrawlPath:涂鸦文件上传的路径,如"scrawls/",实现上传到"{{MEDIA_ROOT}}/scrawls"文件夹,如果不指定则默认=imagepath

    imageManagerPath:图片管理器显示的路径,如"imglib/",实现上传到"{{MEDIA_ROOT}}/imglib",如果不指定则默认=imagepath。

    options:其他UEditor参数,字典类型。参见Ueditor的文档ueditor_config.js里面的说明。

    css:编辑器textarea的CSS样式

    width,height:编辑器的宽度和高度,以像素为单位。

  • 初始化数据库

makemigrationsmigrate
修改settings文件
  • 增加文件上传路径配置

MEDIA_URL='/upload/'MEDIA_ROOT = os.path.join(BASE_DIR, 'upload/')#这个是在浏览器上访问该上传文件的url的前缀
修改xadmin的配置(如果用admin的话可以忽略)
  • 在项目下的xadmin\plugins\路径下新建ueditor.py脚本,内容如下

import xadminfrom xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminViewfrom DjangoUeditor.models import UEditorFieldfrom DjangoUeditor.widgets import UEditorWidgetfrom django.conf import settingsclass 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 = '' % (settings.STATIC_URL + "ueditor/ueditor.config.js")  # 自己的静态目录        js += '' % (settings.STATIC_URL + "ueditor/ueditor.all.js")  # 自己的静态目录        nodes.append(js)xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)
  • 在xadmin\plugins\路径下的init.py文件下的PLUGINS项添加ueditor,如下最后一行为新增的

E:\items\blog_project\xadmin\plugins\__init__.pyPLUGINS = (    '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',)
更改DjangoUeditor静态资源路径(重要)
  • 在项目下的static目录下新建ueditor目录

  • 把DjangoUeditor目录下的ueditor目录下的js文件移动到项目的static目录下的ueditor里

修改项目urls文件
  • 以下为新增项

from django.conf.urls import url,include...import xadminimport  DjangoUeditorurlpatterns = [    url(r'^xadmin/', xadmin.site.urls),...    url(r'^ueditor/', include('DjangoUeditor.urls'))]from django.conf import settingsif settings.DEBUG:    from django.conf.urls.static import static    urlpatterns += static(        settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

关于"django xadmin如何集成DjangoUeditor富文本编辑器"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

0