Django框架使用富文本編輯器Uedit的方法分析
本文實(shí)例講述了Django框架使用富文本編輯器Uedit的方法。分享給大家供大家參考,具體如下:
Uedit是百度一款非常好用的富文本編輯器
一、安裝及基本配置
官方GitHub(有詳細(xì)的安裝使用教程):https://github.com/zhangfisher/DjangoUeditor
1. settings.py
INSTALLED_APPS = [ ... 'DjangoUeditor', ... ]
2. 配置urls
from django.conf.urls import url, include
urlpatterns = [
# 富文本相關(guān)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"文章內(nèi)容")
...
注意,在要使用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中注冊(cè)插件
在extra_apps.xadmin.plugins.__init__.py中添加
PLUGINS = ( ... 'ueditor', )
友情提醒
在Django中使用富文本編輯器
在HTML頁面中,Django處于安全考慮,將文本內(nèi)容默認(rèn)轉(zhuǎn)義,我們需要關(guān)閉
來正常輸出我們的文章
{% autoescape off %}
{{ article.abstract }}
{% endautoescape %}
記錄一下,空格的轉(zhuǎn)義字符分為如下幾種:
1. &160#;不斷行的空白(1個(gè)字符寬度)
2.  &8194#;半個(gè)空白(1個(gè)字符寬度)
3.  &8195#;一個(gè)空白(2個(gè)字符寬度)
4.  &8201#;窄空白(小于1個(gè)字符寬度)
平時(shí)一般用的是 但是在中文中也許有時(shí)候更適合用 
希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python實(shí)現(xiàn)飛機(jī)大戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Python numpy大矩陣運(yùn)算內(nèi)存不足如何解決
這篇文章主要介紹了Python numpy大矩陣運(yùn)算內(nèi)存不足如何解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
python實(shí)現(xiàn)ipsec開權(quán)限實(shí)例
這篇文章主要介紹了python實(shí)現(xiàn)ipsec開權(quán)限的方法,彌補(bǔ)了windows自帶的命令行工具netsh ipsec static add filter不支持批量添加及添加重復(fù)規(guī)則的不足,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11

