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使用PyFetion来发送短信的例子
Apr 22 Python
Python字符串中查找子串小技巧
Apr 10 Python
Python+树莓派+YOLO打造一款人工智能照相机
Jan 02 Python
Python图片转换成矩阵,矩阵数据转换成图片的实例
Jul 02 Python
Python可变和不可变、类的私有属性实例分析
May 31 Python
使用PyCharm进行远程开发和调试的实现
Nov 04 Python
浅谈Python type的使用
Nov 19 Python
pytorch常见的Tensor类型详解
Jan 15 Python
Selenium结合BeautifulSoup4编写简单的python爬虫
Nov 06 Python
Python使用tkinter实现小时钟效果
Feb 22 Python
pyqt5蒙版遮罩mask,setmask的使用
Jun 11 Python
利用python进行数据加载
Jun 20 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
我的论坛源代码(三)
2006/10/09 PHP
php笔记之:初探PHPcms模块开发介绍
2013/04/26 PHP
php输出xml必须header的解决方法
2014/10/17 PHP
laravel框架创建授权策略实例分析
2019/11/22 PHP
利用jQuery简单实现产品展示图片左右滚动功能(示例代码)
2014/01/02 Javascript
动态添加option及createElement使用示例
2014/01/26 Javascript
js实现点击后将文字或图片复制到剪贴板的方法
2014/08/04 Javascript
js匿名函数的调用示例(形式多种多样)
2014/08/20 Javascript
详解js中构造流程图的核心技术JsPlumb(2)
2015/12/08 Javascript
谈谈我对JavaScript中typeof和instanceof的深入理解
2015/12/25 Javascript
AngularJS入门教程之路由机制ngRoute实例分析
2016/12/13 Javascript
Vue 源码分析之 Observer实现过程
2018/03/29 Javascript
Vue在页面右上角实现可悬浮/隐藏的系统菜单
2018/05/04 Javascript
微信小程序时间选择插件使用详解
2018/12/28 Javascript
详解vue实现坐标拾取器功能示例
2020/11/18 Vue.js
[36:09]Secret vs VG 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.24
2019/09/10 DOTA
python创建只读属性对象的方法(ReadOnlyObject)
2013/02/10 Python
详解Django框架中用户的登录和退出的实现
2015/07/23 Python
Python3实现的Mysql数据库操作封装类
2018/06/06 Python
Python基于datetime或time模块分别获取当前时间戳的方法实例
2019/02/19 Python
python 如何将数据写入本地txt文本文件的实现方法
2019/09/11 Python
大家都说好用的Python命令行库click的使用
2019/11/07 Python
如何将你的应用迁移到Python3的三个步骤
2019/12/22 Python
40行Python代码实现天气预报和每日鸡汤推送功能
2020/02/27 Python
意大利宠物用品购物网站:Bauzaar
2018/09/15 全球购物
园林设计专业毕业生求职信
2014/03/23 职场文书
法人代表授权委托书范文
2014/09/10 职场文书
五好文明家庭事迹材料
2014/12/20 职场文书
2015年中秋节演讲稿
2015/03/20 职场文书
党员个人承诺书
2015/04/27 职场文书
2016秋季运动会前导词
2015/11/25 职场文书
2016小学新学期寄语
2015/12/04 职场文书
2019年让高校“心动”的自荐信
2019/03/25 职场文书
Mysql排查分析慢sql之explain实战案例
2022/04/19 MySQL
Python日志模块logging用法
2022/06/05 Python
pytorch实现加载保存查看checkpoint文件
2022/07/15 Python