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实现机器学习之元线性回归

python实现机器学习之元线性回归

一、理论知识准备 1.确定假设函数 如:y=2x+7 其中,(x,y)是一组数据,设共有m个 2.误差cost 用平方误差代价函数 3.减小误差(用梯度下降)...

python实现月食效果实例代码

python实现月食效果实例代码

我们在学习Python当中的pygame模块时,我们都知道我们可以通过使用 pygame模块实现很多功能性的东西,但是很多人应该不知道怎么通过使用pygame实现月食的效果吧,接下来我就...

python3.6使用tkinter实现弹跳小球游戏

本文实例为大家分享了python3.6实现弹跳小球游戏的具体代码,供大家参考,具体内容如下 import random import time from tkinter import...

python mqtt 客户端的实现代码实例

这篇文章主要介绍了python mqtt 客户端代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装paho-mqtt...

Python自动发邮件脚本

Python自动发邮件脚本

缘起 这段时间给朋友搞了个群发邮件的脚本,为了防止进入垃圾邮件,做了很多工作,刚搞完,垃圾邮件进入率50%,觉得还不错,如果要将垃圾邮件的进入率再调低,估计就要花钱买主机了,想想也就算了...