Django集合Ueditor

语言版本环境: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 %} 

 

posted on 2018-01-06 10:18  LOVESTYUDY  阅读(257)  评论(1编辑  收藏  举报

导航