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检测lvs real server状态
Jan 22 Python
python与php实现分割文件代码
Mar 06 Python
教大家玩转Python字符串处理的七种技巧
Mar 31 Python
Python实现的直接插入排序算法示例
Apr 29 Python
Python实现的生产者、消费者问题完整实例
May 30 Python
Python使用pylab库实现绘制直方图功能示例
Jun 01 Python
python使用ddt过程中遇到的问题及解决方案【推荐】
Oct 29 Python
python3利用ctypes传入一个字符串类型的列表方法
Feb 12 Python
PyQt5图形界面播放音乐的实例
Jun 17 Python
Python实现实时数据采集新型冠状病毒数据实例
Feb 04 Python
如何通过命令行进入python
Jul 06 Python
使用tensorflow进行音乐类型的分类
Aug 14 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
一键删除顽固的空文件夹 软件下载
2007/01/26 PHP
生成php程序的php代码
2008/04/07 PHP
PHP实现导出excel数据的类库用法示例
2016/10/15 PHP
phpstorm 正则匹配删除空行、注释行(替换注释行为空行)
2018/01/21 PHP
增强的 JavaScript 的 trim 函数的代码
2007/08/13 Javascript
JavaScript新窗口与子窗口传值详解
2014/02/11 Javascript
JavaScript数值数组排序示例分享
2014/05/27 Javascript
JavaScript+CSS实现仿天猫侧边网页菜单效果
2015/08/25 Javascript
js return返回多个值,通过对象的属性访问方法
2017/02/21 Javascript
Vue-cli创建项目从单页面到多页面的方法
2017/09/20 Javascript
Vue2.0 实现移动端图片上传功能
2018/05/30 Javascript
vue-cli3使用 DllPlugin 实现预编译提升构建速度
2019/04/24 Javascript
jquery分页优化操作实例分析
2019/08/23 jQuery
Python查询阿里巴巴关键字排名的方法
2015/07/08 Python
Python爬虫辅助利器PyQuery模块的安装使用攻略
2016/04/24 Python
python实现SMTP邮件发送功能
2020/06/16 Python
详解Python中的Numpy、SciPy、MatPlotLib安装与配置
2017/11/17 Python
python traceback捕获并打印异常的方法
2018/08/31 Python
Python解决两个整数相除只得到整数部分的实例
2018/11/10 Python
使用Filter过滤python中的日志输出的实现方法
2019/07/17 Python
django template实现定义临时变量,自定义赋值、自增实例
2020/07/12 Python
python re.match()用法相关示例
2021/01/27 Python
德国孕妇装和婴童服装网上商店:bellybutton
2018/04/12 全球购物
Ootori在线按摩椅店:一家专业的按摩椅制造商
2019/04/10 全球购物
精伦电子Java笔试题
2013/01/16 面试题
八年级英语教学反思
2014/01/09 职场文书
大学生创业感言
2014/01/25 职场文书
《金钱的魔力》教学反思
2014/02/24 职场文书
个人四风问题整改措施
2014/10/24 职场文书
单位婚育证明范本
2014/11/21 职场文书
承诺书范本
2015/01/21 职场文书
军事博物馆观后感
2015/06/05 职场文书
2015年暑期社会实践报告
2015/07/13 职场文书
《月球之谜》教学反思
2016/02/20 职场文书
家电创业计划书
2019/08/05 职场文书
SONY600GR,国产收音机厂商永远的痛
2022/04/05 无线电