Django中在xadmin中集成DjangoUeditor过程详解


Posted in Python onJuly 24, 2019

环境

python版本:3.6

django:1.10.8

1.下载xadmin

https://github.com/sshwsfc/xadmin

下载DjangoUeditor

https://github.com/twz915/DjangoUeditor3

2.直接将xadmin和DjangoUeditor集成在pycharm里,在项目下新建一个文件夹extra_apps,将与xadmin、DjangoUeditor的同名文件复制在extra_apps下

Django中在xadmin中集成DjangoUeditor过程详解

3.在settings.py里注册DjangoUeditor

INSTALLED_APPS = [
 ...
 #xadmin第三方插件,实现富文本编辑
 'DjangoUeditor'
]

4.在url里对其进行配置

url(r'^ueditor/',include('DjangoUeditor.urls'))

5.在xadmin中添加插件ueditor

Django中在xadmin中集成DjangoUeditor过程详解

在xadmin-》plugins下新建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.在xadmin-》plugins-》__init__.py中添加ueditor

PLUGINS = (
 'actions',
 'filters',
 'bookmark',
 'export',
 'layout',
 'refresh',
 'details',
 'editable',
 'relate',
 'chart',
 'ajax',
 'relfield',
 'inline',
 'topnav',
 'portal',
 'quickform',
 'wizard',
 'images',
 'auth',
 'multiselect',
 'themes',
 'aggregation',
 'mobile',
 'passwords',
 'sitemenu',
 'language',
 'quickfilter',
 'sortablelist',
 'importexport'
 'ueditor'
)

7.将ueditor添加到adminx.py中

这里的NoticeContent是指使用UEditorField的字段

class NoticeAdmin(object): list_display = ['NoticeTitle', 'NoticeContent','NoticeDesc','NoticeCategory', 'NoticeData','NoticeUser'] style_fields={"NoticeContent":"ueditor"}

8.运行结果:

Django中在xadmin中集成DjangoUeditor过程详解

9.前端显示需要加上:

{% autoescape off %}
{% endautoescape %}

注意:要想在富文本编辑框中显示出图片,必须在settings.py里设置路径:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media').replace('\\', '/')  #设置静态文件路径为主目录下的media文件夹
MEDIA_URL = '/media/'

在与项目同名的文件下的urls.py中添加:

urlpatterns = [
 url('admin/', admin.site.urls),
 url(r'^ueditor/',include('DjangoUeditor.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

否则无法显示图片。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 提取dict转换为xml/json/table并输出的实现代码
Aug 28 Python
Python简单实现安全开关文件的两种方式
Sep 19 Python
python实现读取并显示图片的两种方法
Jan 13 Python
python通过tcp发送xml报文的方法
Dec 28 Python
pycharm新建一个python工程步骤
Jul 16 Python
python实现复制大量文件功能
Aug 31 Python
Python MongoDB 插入数据时已存在则不执行,不存在则插入的解决方法
Sep 24 Python
Python Gluon参数和模块命名操作教程
Dec 18 Python
Python如何对XML 解析
Jun 28 Python
Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取
Jun 30 Python
Python logging日志模块 配置文件方式
Jul 12 Python
Selenium python时间控件输入问题解决方案
Jul 22 Python
Django 权限认证(根据不同的用户,设置不同的显示和访问权限)
Jul 24 #Python
Django 创建/删除用户的示例代码
Jul 24 #Python
python3.6+django2.0+mysql搭建网站过程详解
Jul 24 #Python
简单了解python 邮件模块的使用方法
Jul 24 #Python
python 根据字典的键值进行排序的方法
Jul 24 #Python
如何使用Flask-Migrate拓展数据库表结构
Jul 24 #Python
Python定时任务工具之APScheduler使用方式
Jul 24 #Python
You might like
php调用dll的实例操作动画与代码分享
2012/08/14 PHP
ThinkPHP关联模型操作实例分析
2012/09/23 PHP
PHP实现的增强性mhash函数
2015/05/27 PHP
PHP判断上传文件类型的解决办法
2015/10/20 PHP
PHP解压tar.gz格式文件的方法
2016/02/14 PHP
Yii框架模拟组件调用注入示例
2019/11/11 PHP
PHP xpath提取网页数据内容代码解析
2020/07/16 PHP
js活用事件触发对象动作
2008/08/10 Javascript
javascript 添加和移除函数的通用方法
2009/10/20 Javascript
JavaScript根据数据生成百分比图和柱状图的实例代码
2013/07/14 Javascript
js生成的验证码的实现与技术分析
2014/09/17 Javascript
js超时调用setTimeout和间歇调用setInterval实例分析
2015/01/28 Javascript
原生js结合html5制作简易的双色子游戏
2015/03/30 Javascript
3kb jQuery代码搞定各种树形选择的实现方法
2016/06/10 Javascript
NodeJs测试框架Mocha的安装与使用
2017/03/28 NodeJs
详解Angular 4.x NgIf 的用法
2017/05/22 Javascript
解读ES6中class关键字
2017/11/20 Javascript
详解Vue中localstorage和sessionstorage的使用
2017/12/22 Javascript
nodejs实现的连接MySQL数据库功能示例
2018/01/25 NodeJs
记一次用vue做的活动页的方法步骤
2019/04/11 Javascript
小程序采集录音并上传到后台
2019/11/22 Javascript
[02:07]2018DOTA2亚洲邀请赛主赛事第三日五佳镜头 fy极限反杀
2018/04/06 DOTA
python3编码问题汇总
2016/09/06 Python
python2.7的编码问题与解决方法
2016/10/04 Python
在Linux命令行终端中使用python的简单方法(推荐)
2017/01/23 Python
Python中django学习心得
2017/12/06 Python
Python交互环境下实现输入代码
2018/06/22 Python
python使用matplotlib模块绘制多条折线图、散点图
2020/04/26 Python
python 实现两个线程交替执行
2020/05/02 Python
加热夹克:RAVEAN
2018/10/19 全球购物
商务英语专业应届毕业生求职信
2013/10/28 职场文书
财务出纳员岗位职责
2013/11/26 职场文书
服装仓管员岗位职责
2014/06/17 职场文书
小学生2015教师节演讲稿
2015/03/19 职场文书
《半截蜡烛》教学反思
2016/02/19 职场文书
R9700摩机记
2022/04/05 无线电