django配置Ueditor富文本編輯器


1.https://github.com/twz915/DjangoUeditor3下載包,進入包文件夾,找到DjangoUeditor包拷貝到項目下,和xadmin同級目錄

 

2.找到項目的settings文件,注冊app

 

3.找到項目urls文件,配置DjangoUeditor路由

 

4.找到app下的models,在需要使用富文本框的字段使用UEditorField,相關參數含義可參考文檔:https://github.com/zhangfisher/DjangoUeditor

[python] view plain copy

  1. from DjangoUeditor.models import UEditorField  

[python] view plain copy

  1. content=UEditorField(verbose_name='博客內容',  
  2. width=700,  
  3. height=400,  
  4. toolbars='full',  
  5. imagePath='ueditor/images/',  
  6. filePath='ueditor/files/',  
  7. upload_settings={'imageMaxSizing':1024000},  
  8. default='')  


5.xadmin中添加插件ueditor

由於已經將xadmin源文件拷貝到了項目下,為extra_apps/xadmin,在xadmin下的plugin中新建一個ueditor.py文件,添加以下代碼:

[python] view plain copy

  1. import xadmin  
  2. from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView  
  3. from DjangoUeditor.models import UEditorField  
  4. from DjangoUeditor.widgets import UEditorWidget  
  5. from django.conf import settings  
  6. class XadminUEditorWidget(UEditorWidget):  
  7. def __init__(self,**kwargs):  
  8. self.ueditor_options=kwargs  
  9. self.Media.js = None  
  10. super(XadminUEditorWidget,self).__init__(kwargs)  
  11. class UeditorPlugin(BaseAdminPlugin):  
  12. def get_field_style(self, attrs, db_field, style, **kwargs):  
  13. if style == 'ueditor':  
  14. if isinstance(db_field, UEditorField):  
  15. widget = db_field.formfield().widget  
  16. param = {}  
  17. param.update(widget.ueditor_settings)  
  18. param.update(widget.attrs)  
  19. return {'widget': XadminUEditorWidget(**param)}  
  20. return attrs  
  21. def block_extrahead(self, context, nodes):  
  22. js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js")         #自己的靜態目錄  
  23. js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js")   #自己的靜態目錄  
  24. nodes.append(js)  
  25. xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)  
  26. xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)  

6.將ueditor插件添加到plugins中的__init__.py的PLUGINS中

 

7.找到app下的adminx.py文件,配置插件

 

在頁面上要取消轉義

 

8.配置上傳文件的加載路徑

1.在settings里面配置

[python] view plain copy

  1. MEDIA_URL='/media/'  
  2. MEDIA_ROOT=os.path.join(BASE_DIR,'media')  

[python] view plain copy

  1. settings里面配置  
  2. from django.views.static import serve  
  3. urls里面配置  
  4. url(r'^media/(?P<path>.*)$',serve,{"document_root":settings.MEDIA_ROOT},name='media')  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM