1.https://github.com/twz915/DjangoUeditor3下載包,進入包文件夾,找到DjangoUeditor包拷貝到項目下,和xadmin同級目錄javascript
2.找到項目的settings文件,註冊appjava
3.找到項目urls文件,配置DjangoUeditor路由node
4.找到app下的models,在須要使用富文本框的字段使用UEditorField,相關參數含義可參考文檔:https://github.com/zhangfisher/DjangoUeditorpython
[python] view plain copygit
- from DjangoUeditor.models import UEditorField
[python] view plain copygithub
- content=UEditorField(verbose_name='博客內容',
- width=700,
- height=400,
- toolbars='full',
- imagePath='ueditor/images/',
- filePath='ueditor/files/',
- upload_settings={'imageMaxSizing':1024000},
- default='')
5.xadmin中添加插件ueditordjango
因爲已經將xadmin源文件拷貝到了項目下,爲extra_apps/xadmin,在xadmin下的plugin中新建一個ueditor.py文件,添加如下代碼:app
[python] view plain copyurl
- 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)
6.將ueditor插件添加到plugins中的__init__.py的PLUGINS中spa
7.找到app下的adminx.py文件,配置插件
在頁面上要取消轉義
8.配置上傳文件的加載路徑
1.在settings裏面配置
[python] view plain copy
- MEDIA_URL='/media/'
- MEDIA_ROOT=os.path.join(BASE_DIR,'media')
[python] view plain copy
- 在settings裏面配置
- from django.views.static import serve
- 在urls裏面配置
- url(r'^media/(?P<path>.*)$',serve,{"document_root":settings.MEDIA_ROOT},name='media')