語言版本環境:python3.6
1、win安裝步驟:
1 git下載源碼https://github.com/zhangfisher/DjangoUeditor
2 解壓DjangoUeditor3-master.tar
3 cd C:\Users\fj\Desktop\DjangoUeditor3-master
4 python setup.py install?
官方建議使用pip install?DjangoUeditor
?,但是我使用之后報錯。故自己下載安裝包,手動安裝。大家可以先按官方建議。報錯在進行手動安裝
2、settins.py配置
INSTALLED_APPS中加入'DjangoUeditor'
3、urls.py配置
urlpatterns中加入url(r'^ueditor/',include('DjangoUeditor.urls' )),
4、創建ueditor文件到plugins(xadmin后臺)
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 7 8 class XadminUEditorWidget(UEditorWidget): 9 def __init__(self,**kwargs): 10 self.ueditor_options=kwargs 11 self.Media.js = None 12 super(XadminUEditorWidget,self).__init__(kwargs) 13 14 class UeditorPlugin(BaseAdminPlugin): 15 16 def get_field_style(self, attrs, db_field, style, **kwargs): 17 if style == 'ueditor': 18 if isinstance(db_field, UEditorField): 19 widget = db_field.formfield().widget 20 param = {} 21 param.update(widget.ueditor_settings) 22 param.update(widget.attrs) 23 return {'widget': XadminUEditorWidget(**param)} 24 return attrs 25 26 def block_extrahead(self, context, nodes): 27 js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js") #自己的靜態目錄 28 js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js") #自己的靜態目錄 29 nodes.append(js) 30 31 xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)#修改頁面 32 xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)#新增頁面
5、model修改內容
1 from DjangoUeditor.models import UEditorField 2 class Course(models.Model): 3 desc = models.CharField(max_length=300,verbose_name=u'課程描述') 4 #imagePath 圖片存儲路徑 5 detail = UEditorField(verbose_name = u'課程詳情', width=600, height=300, imagePath="courses/ueditor/", 6 filePath="courses/ueditor/", default='')
?7、配置xadmin/plugins中的init文件(必須添加,否則無法生效)
?在PLUGINS里面添加'ueditor'
8、xadmin添加style_fields
1 from .models import Course 2 class CourseAdmin(object): 3 list_display = ['name', 'desc', 'detail', 'degree','learn_times','studens','click_num','get_zj_nums','go_to']#后臺顯示哪些列 4 search_fields = ['name', 'desc', 'detail', 'degree','studens']# 搜索,搜索中不能添加時間比較麻煩,放在過濾里面 5 list_filter = ['name', 'desc', 'detail', 'degree','learn_times','studens']#過濾 6 ordering = ('-click_num',)#顯示排序 7 readonly_fields = ['click_num']#只讀 后臺不可編輯 8 exclude = ['fav_numbers']#隱藏字段 此字段與readonly_fields互斥 9 inlines = [LessonInLine,CoursesResourceInLine] 10 list_editable = ['degree','desc']#在后臺列表頁面有哪些字段可以修改 11 refresh_times = [3,5] #對列表頁定時刷新,3和5分別代表秒 12 style_fields = {"detail":"ueditor"}#指明某個字段要使用ueditor 13 xadmin.site.register(Course,CourseAdmin)
9、前端頁面調用
1 {% autoescape off %}#關閉轉義
2 {{ course.detail }}
3 {% endautoescape %}?
?