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 相关文章推荐
以windows service方式运行Python程序的方法
Jun 03 Python
python编程开发之textwrap文本样式处理技巧
Nov 13 Python
Python访问MongoDB,并且转换成Dataframe的方法
Oct 15 Python
总结python中pass的作用
Feb 27 Python
使用python搭建服务器并实现Android端与之通信的方法
Jun 28 Python
如何实现Django Rest framework版本控制
Jul 25 Python
Python 线程池用法简单示例
Oct 02 Python
pytorch中的上采样以及各种反操作,求逆操作详解
Jan 03 Python
django 数据库返回queryset实现封装为字典
May 19 Python
PyCharm中关于安装第三方包的三个建议
Sep 17 Python
python 如何停止一个死循环的线程
Nov 24 Python
Python中的协程(Coroutine)操作模块(greenlet、gevent)
May 30 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
利用static实现表格的颜色隔行显示的代码
2007/09/02 PHP
php木马攻击防御之道
2008/03/24 PHP
PHP sprintf() 函数的应用(定义和用法)
2012/06/29 PHP
PHP实现懒加载的方法
2015/03/07 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
2018/02/11 PHP
JavaScript 空位补零实现代码
2010/02/26 Javascript
jQuery中bind与live的用法及区别小结
2014/01/27 Javascript
javascript面向对象特性代码实例
2014/06/12 Javascript
AngularJs基本特性解析(一)
2016/07/21 Javascript
Vue.js动态添加、删除选题的实例代码
2016/09/30 Javascript
bootstrap模态框示例代码分享
2017/05/17 Javascript
vue仿淘宝订单状态的tab切换效果
2020/06/23 Javascript
JS基于for语句编写的九九乘法表示例
2018/01/04 Javascript
解决Layui选择全部,换页checkbox复选框重新勾选的问题方法
2018/08/14 Javascript
JS拖拽排序插件Sortable.js用法实例分析
2019/02/20 Javascript
如何自定义微信小程序tabbar上边框的颜色
2019/07/09 Javascript
[48:32]VGJ.T vs Fnatic 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
Python如何判断数独是否合法
2016/09/08 Python
Python pass详细介绍及实例代码
2016/11/24 Python
Python 3.3实现计算两个日期间隔秒数/天数的方法示例
2019/01/07 Python
window环境pip切换国内源(pip安装异常缓慢的问题)
2019/12/31 Python
python词云库wordCloud使用方法详解(解决中文乱码)
2020/02/17 Python
python自动点赞功能的实现思路
2020/02/26 Python
Python爬取数据并实现可视化代码解析
2020/08/12 Python
可能这些是你想要的H5软键盘兼容方案(小结)
2019/04/23 HTML / CSS
美津浓美国官网:Mizuno美国
2018/08/07 全球购物
shallow copy和deep copy的区别
2016/05/09 面试题
linux面试题参考答案(8)
2015/08/11 面试题
车间班组长岗位职责
2013/11/13 职场文书
2014婚礼司仪主持词
2014/03/14 职场文书
优秀驾驶员先进事迹材料
2014/05/04 职场文书
书法大赛策划方案
2014/06/04 职场文书
拾金不昧锦旗标语
2014/06/27 职场文书
大型公益活动策划方案
2014/08/20 职场文书
跑吧孩子观后感
2015/06/10 职场文书
Windows Server 修改远程桌面端口的实现
2022/06/25 Servers