tinymce富文本编辑器整合到django
第一步:定义表存图片路径
models.py
class AdminIMG(models.Model):filename = models.CharField(max_length=200, blank=True, null=True)img = models.ImageField(upload_to = './admin')
- H:\blog>python manage.py makemigrations
- Migrations for 'XYZblog':
- XYZblog\migrations\0002_auto_20170923_2018.py
- - Create model AdminIMG
- H:\blog>python manage.py migrate
- Operations to perform:
- Apply all migrations: XYZblog, admin, auth, contenttypes, sessions
- Running migrations:
- Applying XYZblog.0002_auto_20170923_2018... OK
第二步:定义视图,让图片直接显示在编辑框内
views.py
from .models import AdminIMGdef uploadIMG(request):img = request.FILES.get('img')adminIMG = AdminIMG()adminIMG.filename = img.nameadminIMG.img = imgadminIMG.save()return HttpResponse("<script>top.$('.mce-btn.mce-open').parent().find('.mce-textbox').val('/media/%s').closest('.mce-window').find('.mce-primary').click();</script>" % adminIMG.img)
第三步:定义上传的目录
settings.py
- MEDIA_ROOT = os.path.join(BASE_DIR, 'static/upload/img')
第四步:定义url
urls.py
from django.views.static import servefrom blog.settings import MEDIA_ROOTurlpatterns = [url(r'uploadIMG/',views.uploadIMG,name='uploadIMG'),url(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT, }),......]
第五步:编辑\static\js\tinymce\textareas.js
- tinymce.init({
- selector: 'textarea',
- theme : "modern",
- plugins: ["image"],
- image_advtab: true,
- paste_data_images:true,
- file_browser_callback: function(field_name, url, type, win) {
- if(type=='image') $('#my_form input').click();
- },
- });
- $( document ).ready(function() {
- h ='<iframe id="form_target" name="form_target" style="display:none"></iframe><form id="my_form" action="/temporary/uploadIMG/" target="form_target" method="post" enctype="multipart/form-data" style="width:0px;height:0;overflow:hidden"><input name="img" type="file" onchange="$(\'#my_form\').submit();this.value=\'\';"></form>';
- $('body').append(h);
- function getCookie(name) {
- var cookieValue = null;
- if (document.cookie && document.cookie != '') {
- var cookies = document.cookie.split(';');
- for (var i = 0; i < cookies.length; i++) {
- var cookie = jQuery.trim(cookies[i]);
- // Does this cookie string begin with the name we want?
- if (cookie.substring(0, name.length + 1) == (name + '=')) {
- cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
- break;
- }
- }
- }
- return cookieValue;
- }
- var csrftoken = getCookie('csrftoken');
- console.log(csrftoken);
- $('#my_form').append('<input type="hidden" name="csrfmiddlewaretoken" value='+csrftoken+' />');
- });
编辑器已有图片上传功能:
保存,查看数据库:
查看上传目录:
查看前台显示:
OK!
tinymce富文本编辑器整合到django的更多相关文章
- Vue CLI 3+tinymce 5富文本编辑器整合
基于Vue CLI 3脚手架搭建的项目整合tinymce 5富文本编辑器,vue cli 2版本及tinymce 4版本参考:https://blog.csdn.net/liub37/article/ ...
- 15、Vue CLI 3+tinymce 5富文本编辑器整合
富文本编辑器里大佬们都说tinymce NB! 插件安装 inymce官方提供了一个vue的组件tinymce-vue 如果有注册或购买过服务的话,直接通过组件配置api-key直接使用,懒的注册或者 ...
- vue中引入Tinymce富文本编辑器
最近想在项目上引入一个富文本编辑器,之前引入过summernote,感觉并不太适合vue使用, 然后在网上查了查,vue中使用Tinymce比较适合, 首先,我们在vue项目的components文件 ...
- 在 Vue 项目中引入 tinymce 富文本编辑器
项目中原本使用的富文本编辑器是 wangEditor,这是一个很轻量.简洁编辑器 但是公司的业务升级,想要一个功能更全面的编辑器,我找了好久,目前常见的编辑器有这些: UEditor:百度前端的开源项 ...
- tinymce 富文本编辑器 编写资料
tinymce官方文档: 粘贴图片插件 博客搬运地址 使用Blob获取图片并二进制显示实例页面 tinymce自动调整插件 是时候掌握一个富文本编辑器了——TinyMCE(1) XMLHttpRequ ...
- Vue集成tinymce富文本编辑器并实现本地化指南(2019.11.21最新)
tinymce是一款综合口碑特别好.功能异常强大的富文本编辑器,在某些网站,甚至享有"宇宙最强富文本编辑器"的称号.那么,在Vue项目中如何集成呢?这并不困难,只需要参照官方教程 ...
- 「newbee-mall新蜂商城开源啦」 页面优化,最新版 wangEditor 富文本编辑器整合案例
大家比较关心的新蜂商城 Vue3 版本目前已经开发了大部分内容,相信很快就能够开源出来让大家尝鲜了,先让大家看看当前的开发进度: 开源仓库地址为 https://github.com/newbee-l ...
- HandyEditor 富文本编辑器整合到python flask项目中
1.下载HandyEditor,地址http://he.catfish-cms.com/ 2.解压后的文件名HandyEditor-master改为HandyEditor,文件夹里的文件如下 3.将H ...
- Django使用tinymce富文本编辑器
1 - 安装 pip install django-tinymce==2.6.0 2 - 注册app INSTALLED_APPS = ( ... 'tinymce', ) 3 - 在setting中 ...
随机推荐
- Python Requests-学习笔记(4)-定制请求头和POST
定制请求头 如果你想为请求添加HTTP头部,只要简单地传递一个 dict 给 headers 参数就可以了. import jsonurl = 'https://api.github.com/some ...
- 初探CI,Github调戏Action手记——自动构建并发布
前言 最近在做脚本的说明文档时使用了vuepress这个东西 前端实在是菜,只能随便写写了 正常写完md文件之后推送至github做版本控制 而前端页面的生成则是在本地,部署也是在本地手工进行 一套下 ...
- mysql 向上取整
SELECT CEILING(10.0) --->10 SELECT CEILING(10.1) --->11
- 使用jquery清空input 文本框中的内容
只需要将文本框的值置为空即可: function resetBtn(){ $("#name").val(""); }
- 【论文研读】强化学习入门之DQN
最近在学习斯坦福2017年秋季学期的<强化学习>课程,感兴趣的同学可以follow一下,Sergey大神的,有英文字幕,语速有点快,适合有一些基础的入门生. 今天主要总结上午看的有关DQN ...
- Redis的三大问题
一般我们对缓存读操作的时候有这么一个固定的套路: 如果我们的数据在缓存里边有,那么就直接取缓存的. 如果缓存里没有我们想要的数据,我们会先去查询数据库,然后将数据库查出来的数据写到缓存中. 最后将数据 ...
- 最简单的懒人springcloud之Eureka(服务注册与发现)
本文开发软件是STS,是eclipse为springboot项目而生的一个软件,用这个软件开发spring的项目版本都会自己对应的,话不多说直接上代码 springboot版本2.1.8.RELEAS ...
- Android App安全渗透测试(一)
一. 实验环境搭建 1. 安装JDK 2. 安装Android Studio 3. 模拟器或真机 我的是夜神模拟器和nexus 工具 Apktool ...
- C#客户端打印条形码
第一种方法: 引用第三方插件文件zxing.dll // 1.设置条形码规格 EncodingOptions encodeOption = new EncodingOptions(); encodeO ...
- Elasticsearch 集群部署
本文部署环境 $ cat /etc/redhat-release CentOS Linux release (Core) 部署前系统优化 $ /etc/security/limits.conf roo ...