Django项目富文本编辑器DjangoUeditor如何用?扩展属性有哪些?

2023-02-0109:11:52后端程序开发Comments982 views字数 2633阅读模式

1. DjangoUeditor是什么?

Ueditor HTML编辑器是百度开源的在线HTML编辑器,功能非常强大,像表格可以直接拖动调整单元格大小等, DjangoUeditor是把此编辑器集成为可以在django项目中直接使用的app,让django项目可以方便的使用这个编辑器。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

2. 如何获取DjangoUeditor

可以从官方Github下载,然后放到自己的项目里,官方Github地址:https://github.com/zhangfisher/DjangoUeditor, 但是由于官方很久没有更新了,目前只支持Python2.7, 如果要用Python3.6,需要修改才能编译成功,好消息是不需要你自己去修改,慕课网的老师已经修改好了,兼容Python2.7和3.6,可以直接拿来用,这就是开源的神奇,好东西就应该收藏,我也fork到了自己的github,地址:https://github.com/KenZP/DjangoUeditor3_imooc,无限期有效,可以去下载使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

3. 如何安装DjangoUeditor

a. 在自己的项目里,和apps同层级创建一个extra_apps,这个文件存放所有第三方app,把下载好的DjangoUeditor,放到extra_apps里面。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

b. 配置项目setting.py文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

把extra_apps增加到根目录下,加入如下代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))

在INSTALLED_APPS里面增加如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

'DjangoUeditor'

c. url.py配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

在项目url.py文件的urlpatterns里面增加如下url:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

url(r'^ueditor/', include('DjangoUeditor.urls')),

至此,DjangoUeditor已经安装完成,我们就可以使用了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

4. 如何使用DjangoUedit

安装好后,我们只需要在model里面修改需要使用此插件的字段即可,如文章的内容字段,默认如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

body = models.TextField(default="", verbose_name="文章内容")

如果要使用Ueditor,只需要修改如下即可:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

body = UEditorField(default="", verbose_name="文章内容")

因为使用了UEditorField,所以需要import这个模块文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

from DjangoUeditor.models import UEditorField

可以发现,完全和django model字段无缝连接,而且还扩展了很多属性,只需要加上自己需要的属性就可以。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

这样配置完后对数据库做下makemigrations和migrate,进入后台就发现文章内容输入框已经是Ueditor界面了,有很多丰富的功能,如图:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

Django项目富文本编辑器DjangoUeditor如何用?扩展属性有哪些? Django项目富文本编辑器DjangoUeditor如何用?扩展属性有哪些?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html

5. DjangoUeditor扩展属性有哪些?

  • width,height :编辑器的宽度和高度,以像素为单位。
  • toolbars :配置你想显示的工具栏,取值为mini,normal,full,代表小,一般,全部。如果默认的工具栏的按钮数量不符合您的要求,您可以在settings里面配置自己的显示按钮。参见后面介绍。
  • imagePath :图片上传后保存的路径,如"images/",实现上传到"{{MEDIA_ROOT}}/images"文件夹。 注意:如果imagePath值只设置文件夹,则未尾要有"/" imagePath可以按python字符串格式化:如"images/%(basename)s_%(datetime)s.%(extname)s",这样如果上传test.png,则文件会 被保存为"{{MEDIA_ROOT}}/images/test_20140625122399.png",imagePath中可以使用的变量有:
    • time :上传时的时间,datetime.datetime.now().strftime("%H%M%S")
    • date : 上传时的日期,datetime.datetime.now().strftime("%Y%m%d")
    • datetime : 上传时的时间和日期,datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    • year : 年
    • month : 月
    • day : 日
    • rnd : 三位随机数,random.randrange(100,999)
    • basename : 上传的文件名称,不包括扩展名
    • extname : 上传的文件扩展名
    • filename : 上传的文件名全称
  • filePath : 附件上传后保存的路径,设置规则与imagePath一样。
  • upload_settings : 字典值,例:upload_settings={ imagePathFormat:"images/%(basename)s_%(datetime)s.%(extname)s", imageMaxSize:323232 fileManagerListPath:"files" }
    • upload_settings的内容就是ueditor/php/config.json里面的配置内容,因此,你可以去看config.json或者官方文档内容来决定 该如何配置upload_settings,基本上就是用来配置上传的路径、允许上传的文件扩展名、最大上传的文件大小之类的。
    • 上面的imagePath和filePath被单独提取出来配置,原因是因为这两个参数是最常使用到的,imagePath就相当于upload_settings里面的 imagePathFormat,filePath就相当于upload_settings里面的filePathFormat。
    • 您upload_settings里面设置了imagePathFormat,也可以在UeditorField里面设置imagePath,效果是一样的。 但是如果两者均设置, 则imagePath优先级更高。
    • 涂鸦文件、截图、远程抓图、图片库的xxxxPathFormat如果没有配置,默认等于imagePath.
    • 远程文件库的xxxxPathFormat如果没有配置,默认等于filePath.
  • settings : 字典值,配置项与ueditor/ueditor.config.js里面的配置项一致。
  • command : 可以为Ueditor新增一个按钮、下拉框、对话框
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/30586.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/30586.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定