Django-xadmin后台配置富文本编辑器(方法一)
程序员文章站
2022-03-20 22:04:13
1.https://github.com/twz915/DjangoUeditor3下载包,进入包文件夹,找到DjangoUeditor包拷贝到项目下,和xadmin同级目录 2.找到项目的settings文件,注册app 3.找到项目urls文件,配置DjangoUeditor路由 4.找到app ......
1.https://github.com/twz915/djangoueditor3下载包,进入包文件夹,找到djangoueditor包拷贝到项目下,和xadmin同级目录
2.找到项目的settings文件,注册app
3.找到项目urls文件,配置djangoueditor路由
4.找到app下的models,在需要使用富文本框的字段使用ueditorfield,相关参数含义可参考文档:https://github.com/zhangfisher/djangoueditor
from djangoueditor.models import ueditorfield
content=ueditorfield(verbose_name='博客内容',width=700,height=400,toolbars='full',imagepath='ueditor/images/',filepath='ueditor/files/',upload_settings{'imagemaxsizing':1024000},default='')
5.xadmin中添加插件ueditor
由于已经将xadmin源文件拷贝到了项目下,为extra_apps/xadmin,在xadmin下的plugin中新建一个ueditor.py文件,添加以下代码:
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中
7.找到app下的adminx.py文件,配置插件
8.配置上传文件的加载路径
1.在settings里面配置
media_url='/media/' media_root=os.path.join(base_dir,'media') #在settings里面配置 from django.views.static import serve #在urls里面配置 url(r'^media/(?p<path>.*)$',serve,{"document_root":settings.media_root},name='media')