Django中在xadmin中集成DjangoUeditor過(guò)程詳解
環(huán)境
python版本:3.6
django:1.10.8
1.下載xadmin
https://github.com/sshwsfc/xadmin
下載DjangoUeditor
https://github.com/twz915/DjangoUeditor3
2.直接將xadmin和DjangoUeditor集成在pycharm里,在項(xiàng)目下新建一個(gè)文件夾extra_apps,將與xadmin、DjangoUeditor的同名文件復(fù)制在extra_apps下

3.在settings.py里注冊(cè)DjangoUeditor
INSTALLED_APPS = [ ... #xadmin第三方插件,實(shí)現(xiàn)富文本編輯 'DjangoUeditor' ]
4.在url里對(duì)其進(jìn)行配置
url(r'^ueditor/',include('DjangoUeditor.urls'))
5.在xadmin中添加插件ueditor

在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") #自己的靜態(tài)目錄
js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js") #自己的靜態(tài)目錄
nodes.append(js)
xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)
xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)
6.在xadmin-》plugins-》__init__.py中添加ueditor
PLUGINS = ( 'actions', 'filters', 'bookmark', 'export', 'layout', 'refresh', 'details', 'editable', 'relate', 'chart', 'ajax', 'relfield', 'inline', 'topnav', 'portal', 'quickform', 'wizard', 'images', 'auth', 'multiselect', 'themes', 'aggregation', 'mobile', 'passwords', 'sitemenu', 'language', 'quickfilter', 'sortablelist', 'importexport' 'ueditor' )
7.將ueditor添加到adminx.py中
這里的NoticeContent是指使用UEditorField的字段
class NoticeAdmin(object): list_display = ['NoticeTitle', 'NoticeContent','NoticeDesc','NoticeCategory', 'NoticeData','NoticeUser'] style_fields={"NoticeContent":"ueditor"}
8.運(yùn)行結(jié)果:

9.前端顯示需要加上:
{% autoescape off %}
{% endautoescape %}
注意:要想在富文本編輯框中顯示出圖片,必須在settings.py里設(shè)置路徑:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/') #設(shè)置靜態(tài)文件路徑為主目錄下的media文件夾
MEDIA_URL = '/media/'
在與項(xiàng)目同名的文件下的urls.py中添加:
urlpatterns = [
url('admin/', admin.site.urls),
url(r'^ueditor/',include('DjangoUeditor.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
否則無(wú)法顯示圖片。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 實(shí)現(xiàn)輸入任意多個(gè)數(shù),并計(jì)算其平均值的例子
今天小編就為大家分享一篇Python 實(shí)現(xiàn)輸入任意多個(gè)數(shù),并計(jì)算其平均值的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Python?SQLAlchemy庫(kù)的實(shí)現(xiàn)示例
SQLAlchemy庫(kù)是一個(gè)強(qiáng)大的工具,為開(kāi)發(fā)人員提供了便捷的方式來(lái)處理與數(shù)據(jù)庫(kù)的交互,本文主要介紹了Python?SQLAlchemy庫(kù)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
Python3爬蟲(chóng)里關(guān)于Splash負(fù)載均衡配置詳解
在本篇文章里小編給大家分享了關(guān)于Python3爬蟲(chóng)里關(guān)于Splash負(fù)載均衡配置的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-07-07
python開(kāi)發(fā)之thread線程基礎(chǔ)實(shí)例入門
這篇文章主要介紹了python開(kāi)發(fā)之thread線程基礎(chǔ),以三個(gè)實(shí)例形式分析了Python中thread線程的基本使用方法,涉及串行與并行程序的執(zhí)行原理及線程的操作技巧,需要的朋友可以參考下2015-11-11
Python實(shí)現(xiàn)簡(jiǎn)單的列表冒泡排序和反轉(zhuǎn)列表操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單的列表冒泡排序和反轉(zhuǎn)列表操作,涉及Python列表遍歷、排序、追加等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07
用Python實(shí)現(xiàn)將一張圖片分成9宮格的示例
今天小編就為大家分享一篇用Python實(shí)現(xiàn)將一張圖片分成9宮格的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07

