第一步:定义表存图片路径

models.py

  1. class AdminIMG(models.Model):
  2.     filename = models.CharField(max_length=200, blank=True, null=True)
  3.     img  = models.ImageField(upload_to = './admin')

  1. H:\blog>python manage.py makemigrations
  2. Migrations for 'XYZblog':
  3. XYZblog\migrations\0002_auto_20170923_2018.py
  4. - Create model AdminIMG
  5. H:\blog>python manage.py migrate
  6. Operations to perform:
  7. Apply all migrations: XYZblog, admin, auth, contenttypes, sessions
  8. Running migrations:
  9. Applying XYZblog.0002_auto_20170923_2018... OK

第二步:定义视图,让图片直接显示在编辑框内

views.py

  1. from .models import AdminIMG
  2. def uploadIMG(request):
  3.     img = request.FILES.get('img')
  4.     adminIMG = AdminIMG()
  5.     adminIMG.filename = img.name
  6.     adminIMG.img = img
  7.     adminIMG.save()
  8.     return HttpResponse(
  9.         "<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

 


  1. MEDIA_ROOT = os.path.join(BASE_DIR, 'static/upload/img')

第四步:定义url

urls.py

  1. from django.views.static import serve
  2. from blog.settings import MEDIA_ROOT
  3. urlpatterns = [
  4.     url(r'uploadIMG/',views.uploadIMG,name='uploadIMG'),
  5.     url(r'^media/(?P<path>.*)$', serve, {'document_root': MEDIA_ROOT, }),
  6.     ......
  7. ]

 

 

 第五步:编辑\static\js\tinymce\textareas.js


  1. tinymce.init({
  2. selector: 'textarea',
  3. theme : "modern",
  4. plugins: ["image"],
  5. image_advtab: true,
  6. paste_data_images:true,
  7. file_browser_callback: function(field_name, url, type, win) {
  8. if(type=='image') $('#my_form input').click();
  9. },
  10. });
  11. $( document ).ready(function() {
  12. 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>';
  13. $('body').append(h);
  14. function getCookie(name) {
  15. var cookieValue = null;
  16. if (document.cookie && document.cookie != '') {
  17. var cookies = document.cookie.split(';');
  18. for (var i = 0; i < cookies.length; i++) {
  19. var cookie = jQuery.trim(cookies[i]);
  20. // Does this cookie string begin with the name we want?
  21. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  22. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  23. break;
  24. }
  25. }
  26. }
  27. return cookieValue;
  28. }
  29. var csrftoken = getCookie('csrftoken');
  30. console.log(csrftoken);
  31. $('#my_form').append('<input type="hidden" name="csrfmiddlewaretoken" value='+csrftoken+' />');
  32. });

编辑器已有图片上传功能:

保存,查看数据库:

查看上传目录:

查看前台显示:

OK!

tinymce富文本编辑器整合到django的更多相关文章

  1. Vue CLI 3+tinymce 5富文本编辑器整合

    基于Vue CLI 3脚手架搭建的项目整合tinymce 5富文本编辑器,vue cli 2版本及tinymce 4版本参考:https://blog.csdn.net/liub37/article/ ...

  2. 15、Vue CLI 3+tinymce 5富文本编辑器整合

    富文本编辑器里大佬们都说tinymce NB! 插件安装 inymce官方提供了一个vue的组件tinymce-vue 如果有注册或购买过服务的话,直接通过组件配置api-key直接使用,懒的注册或者 ...

  3. vue中引入Tinymce富文本编辑器

    最近想在项目上引入一个富文本编辑器,之前引入过summernote,感觉并不太适合vue使用, 然后在网上查了查,vue中使用Tinymce比较适合, 首先,我们在vue项目的components文件 ...

  4. 在 Vue 项目中引入 tinymce 富文本编辑器

    项目中原本使用的富文本编辑器是 wangEditor,这是一个很轻量.简洁编辑器 但是公司的业务升级,想要一个功能更全面的编辑器,我找了好久,目前常见的编辑器有这些: UEditor:百度前端的开源项 ...

  5. tinymce 富文本编辑器 编写资料

    tinymce官方文档: 粘贴图片插件 博客搬运地址 使用Blob获取图片并二进制显示实例页面 tinymce自动调整插件 是时候掌握一个富文本编辑器了——TinyMCE(1) XMLHttpRequ ...

  6. Vue集成tinymce富文本编辑器并实现本地化指南(2019.11.21最新)

     tinymce是一款综合口碑特别好.功能异常强大的富文本编辑器,在某些网站,甚至享有"宇宙最强富文本编辑器"的称号.那么,在Vue项目中如何集成呢?这并不困难,只需要参照官方教程 ...

  7. 「newbee-mall新蜂商城开源啦」 页面优化,最新版 wangEditor 富文本编辑器整合案例

    大家比较关心的新蜂商城 Vue3 版本目前已经开发了大部分内容,相信很快就能够开源出来让大家尝鲜了,先让大家看看当前的开发进度: 开源仓库地址为 https://github.com/newbee-l ...

  8. HandyEditor 富文本编辑器整合到python flask项目中

    1.下载HandyEditor,地址http://he.catfish-cms.com/ 2.解压后的文件名HandyEditor-master改为HandyEditor,文件夹里的文件如下 3.将H ...

  9. Django使用tinymce富文本编辑器

    1 - 安装 pip install django-tinymce==2.6.0 2 - 注册app INSTALLED_APPS = ( ... 'tinymce', ) 3 - 在setting中 ...

随机推荐

  1. MySQL学习之路4-数据的导入导出

    数据的导入 通过数据库管理工具,先建表,然后导入表记录. 通过sql语句导入: load data local infile '表路径' into table stuscore fields term ...

  2. 更快地访问stackoverflow

    使用火狐浏览器,安装扩展组件 Decentraleyes, 完成 原理:由于爆栈本身并没有被墙, 但使用了google的api,而google的api是被墙的. 该组件替换了国内不能访问的api,所以 ...

  3. git以及gitHub的使用说明书

    一.使用说明 1.Git与github的功能: Git是世界上最先进的分布式版本控制系统,也就是用来记录你的项目代码历史变更信息的工具:github就是用来存储你的代码以及变更信息的云端平台: 2.优 ...

  4. stand up meeting 1/11/2016

    part 组员                工作              工作耗时/h 明日计划 工作耗时/h    UI 冯晓云 跑通打印机功能,尝试与pdf读取部分结合;生词本卡片选择简略释义 ...

  5. Android | 教你如何在安卓上实现二代身份证识别,一键实名认证

    @ 目录 前言 场景 开发前准备 android studio 安装 在项目级gradle里添加华为maven仓 在应用级的build.gradle里面加上SDK依赖 在AndroidManifest ...

  6. codeforces Equalizing by Division (easy version)

    output standard output The only difference between easy and hard versions is the number of elements ...

  7. Kaggle入门——泰坦尼克号生还者预测

    前言 这个是Kaggle比赛中泰坦尼克号生存率的分析.强烈建议在做这个比赛的时候,再看一遍电源<泰坦尼克号>,可能会给你一些启发,比如妇女儿童先上船等.所以是否获救其实并非随机,而是基于一 ...

  8. 详解 Properties类

    (请观看本人博文--<详解 I/O流>) Properties类: 概念: Properties 类的对象 是 一个持久的属性集 Properties 可 保存在流中 或 从流中加载 属性 ...

  9. [De1CTF 2019]SSRF Me

    原帖地址 : https://xz.aliyun.com/t/5927 De1CTF 2019 的一个题目总结 题目描述 直接查看页面源代码可以看到正确格式的代码 #! /usr/bin/env py ...

  10. [XML] XML格式【有道翻译】API 的数据转化输出

    <?php header("content-type:text/html;charset=utf-8"); //echo "飞飞仔超级智障"; $cont ...