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查找第k小元素代码分享
Dec 18 Python
Python的Flask框架中@app.route的用法教程
Mar 31 Python
Python实现备份MySQL数据库的方法示例
Jan 11 Python
Python 25行代码实现的RSA算法详解
Apr 10 Python
关于django 数据库迁移(migrate)应该知道的一些事
May 27 Python
python使用xlrd和xlwt读写Excel文件的实例代码
Sep 05 Python
python分块读取大数据,避免内存不足的方法
Dec 10 Python
Python利用lxml模块爬取豆瓣读书排行榜的方法与分析
Apr 15 Python
python 实现提取log文件中的关键句子,并进行统计分析
Dec 24 Python
PYcharm 激活方法(推荐)
Mar 23 Python
Python能做什么
Jun 02 Python
OpenCV 之按位运算举例解析
Jun 19 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 zip文件解压类代码
2009/12/02 PHP
PHP实现对站点内容外部链接的过滤方法
2014/09/10 PHP
php json中文编码为null的解决办法
2016/12/14 PHP
PHP中的self关键字详解
2019/06/23 PHP
php 使用expat方式解析xml文件操作示例
2019/11/26 PHP
提高代码性能技巧谈—以创建千行表格为例
2006/07/01 Javascript
一个简单的jquery进度条示例
2014/04/28 Javascript
Internet Explorer 11 浏览器介绍:别叫我IE
2014/09/28 Javascript
JavaScript使用Math.Min返回两个数中较小数的方法
2015/04/06 Javascript
js全选按钮的实现方法
2015/11/17 Javascript
javascript中checkbox使用方法实例演示
2015/11/19 Javascript
JS模拟简易滚动条效果代码(附demo源码)
2016/04/05 Javascript
jQuery实现发送验证码并60秒倒计时功能
2016/11/25 Javascript
jQuery扩展实现text提示还能输入多少字节的方法
2016/11/28 Javascript
js 颜色选择插件
2017/01/23 Javascript
Vue计算属性的学习笔记
2017/03/22 Javascript
JS中appendChild追加子节点无效的解决方法
2018/10/14 Javascript
微信小程序使用template标签实现五星评分功能
2018/11/03 Javascript
JS/HTML5游戏常用算法之路径搜索算法 A*寻路算法完整实例
2018/12/14 Javascript
es6 filter() 数组过滤方法总结
2019/04/03 Javascript
jQuery实现简单全选框
2020/09/13 jQuery
定制FileField中的上传文件名称实例
2017/08/23 Python
浅谈Django的缓存机制
2018/08/23 Python
Python  unittest单元测试框架的使用
2018/09/08 Python
使用 Python 玩转 GitHub 的贡献板(推荐)
2019/04/04 Python
用pyqt5 给按钮设置图标和css样式的方法
2019/06/24 Python
python 基于TCP协议的套接字编程详解
2019/06/29 Python
使用Django搭建web服务器的例子(最最正确的方式)
2019/08/29 Python
python中的TCP(传输控制协议)用法实例分析
2019/11/15 Python
在spyder IPython console中,运行代码加入参数的实例
2020/04/20 Python
Dyson加拿大官方网站:购买戴森吸尘器,风扇,冷热器及配件
2016/10/26 全球购物
eDreams巴西:廉价机票,酒店优惠和度假套餐
2017/04/14 全球购物
俄罗斯品牌服装在线商店:VIPAVENUE
2020/08/10 全球购物
公司优秀员工推荐信
2015/03/24 职场文书
2015年社区平安建设工作总结
2015/05/13 职场文书
Pandas 数据编码的十种方法
2022/04/20 Python