xadmin 插件开发
Posted chenyang13677
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xadmin 插件开发相关的知识,希望对你有一定的参考价值。
xadmin 插件开发
互文本编辑器,UEditor 在github上面搜索djangoueditor,经行安装下载
插件官方文档 网址:http://xadmin.readthedocs.io/en/docs-chinese/plugins.html#
安装流程:
1. pip install DjangoUeditor
2.settings.py 中加入DjangoUeditor
3.在adminx.py文件里面,courseAdmin类里面加入
def save_models(self):
obj = self.new_obj
obj.save()
if obj.course_org is not None:
course_org = obj.course_org
course_org.course_nums = Courses.objects.filter(course_org=course_org).count()
course_org.save()
4.在models里面填入
from DjangoUeditor.models import UEdiitorField
修改该类中的字段内容
detail = UEditorField(verbose_name=u‘课程详情‘,width=600, height=300, imagePath="courser/ueditor/", filePath="courser/ueditor/", default=‘‘)
3.url(r‘ueditor/‘,include(‘DjangoUeditor.urls‘))
在经行这一步时,会报错,需要修改源码
修改DjangoUeditor\urls.py文件中的,
第4行 将from django.conf.urls import patterns, url 改为 from django.conf.urls import url, include
第10行 将
urlpatterns = patterns(‘‘,
url(r‘^controller/$‘,get_ueditor_controller)
)
改为:
urlpatterns = [
url(r‘^controller/$‘,get_ueditor_controller),
]
在需要修改的app里面的models里面的需要添加ueditor的字段修改为以下字段
4.detai = UEditorField()
5.在\extra_apps\xadmin\plugins下,添加一个文件 ueditor.py
在文件里面填写
# coding=utf-8
import xadmin
from django.db.models import TextField
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(selfself,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.在adminx.py 中,在该类下面添加
style_fields = {"detail":"ueditor"}
7.在\extra_apps\xadmin\plugins下的__init__.py文件中,将‘ueditor‘其添加在后面
8.在前端页面里面 {% autoescape off %} {% endautoescape %}
以上是关于xadmin 插件开发的主要内容,如果未能解决你的问题,请参考以下文章