码迷,mamicode.com
首页 > 其他好文 > 详细

Django中使用富文本编辑器Uedit

时间:2017-07-04 20:20:18      阅读:1723      评论:0      收藏:0      [点我收藏+]

标签:src   path   mode   conf   信息   strong   ase   type   detail   

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中使用富文本编辑器Uedit

标签:src   path   mode   conf   信息   strong   ase   type   detail   

原文地址:http://www.cnblogs.com/wongbingming/p/7117957.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!