Django框架使用富文本编辑器Uedit的方法分析

yipeiwu_com6年前Python基础

本文实例讲述了Django框架使用富文本编辑器Uedit的方法。分享给大家供大家参考,具体如下:

Uedit是百度一款非常好用的富文本编辑器

一、安装及基本配置

官方GitHub(有详细的安装使用教程):https://github.com/zhangfisher/DjangoUeditor

1. settings.py

INSTALLED_APPS = [
  ...
  'DjangoUeditor',
  ...
]

2. 配置urls

from django.conf.urls import url, include
urlpatterns = [
# 富文本相关url
  url(r'^ueditor/', include('DjangoUeditor.urls')),
]

3. 字段信息

在需要使用富文本的字段所在的models.py中

from DjangoUeditor.models import UEditorField
class Articles(models.Model):
  ...
  content = UEditorField(width=1200, height=600, imagePath="article/ueditor/",
              filePath="article/ueditor/",verbose_name=u"文章内容")
  ...

注意,在要使用ueditor的字段所在adminx.py的类中,添加

# 这样就指定了course的detail字段使用ueditor富文本编辑器
class ArticlesAdmin(object):
  ...
  style_fields = {"content":"ueditor"}

二、Ueditor插件制作

1. 插件代码

在extra_apps.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)

2. xadmin中注册插件

在extra_apps.xadmin.plugins.__init__.py中添加

PLUGINS = (
  ...
  'ueditor',
)

友情提醒

在Django中使用富文本编辑器

在HTML页面中,Django处于安全考虑,将文本内容默认转义,我们需要关闭

来正常输出我们的文章

{% autoescape off %}
{{ article.abstract }}
{% endautoescape %}

记录一下,空格的转义字符分为如下几种:

1.  &160#;不断行的空白(1个字符宽度)

2. &8194#;半个空白(1个字符宽度)

3. &8195#;一个空白(2个字符宽度)

4. &8201#;窄空白(小于1个字符宽度)

平时一般用的是 但是在中文中也许有时候更适合用

希望本文所述对大家基于Django框架的Python程序设计有所帮助。

相关文章

python 文件操作api(文件操作函数)

python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块。 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下...

python pyenv多版本管理工具的使用

python pyenv多版本管理工具的使用

项目地址github pyenv does... 改变每个用户系统级别的 python 版本 为每个项目提供不同的 python 版本 安装 克隆到本地即为安装,默认目录是...

Python的自动化部署模块Fabric的安装及使用指南

fabric是python2.5或者更高的库,可以通过ssh在多个host上批量执行任务.完成系统管理任务.它提供一套基本操作在本地和远程执行shell命令,或者上传下载文件,辅助提供用...

python使用rpc框架gRPC的方法

概述 gRPC 是谷歌开源的一个rpc(远程程序调用)框架,可以轻松实现跨语言,跨平台编程,其采用gRPC协议(基于HTTP2)。 rpc: remote procedure call...

python opencv鼠标事件实现画框圈定目标获取坐标信息

本文实例为大家分享了python-opencv鼠标事件画框圈定目标的具体代码,供大家参考,具体内容如下 在视频/相机中,用鼠标画矩形框,圈定目标,从而获得鼠标的起始坐标点a、终止坐标点b...