[原创]Python/Django使用富文本编辑器XHeditor上传本地图片
前言
为了在Django框架下使用Xheditor上传图片,居然折腾了我一个晚上。期间也遇到种种问题,网上相关资料极少。现在把经验分享给大家。
正文
xheditor篇
2.将其中的xheditor-zh-cn.min.js以及xheditor_emot、xheditor_plugins和xheditor_skin三个文件夹copy到xheditor目录下。注:如果您网站中没有使用jQuery框架,也请一并上传jquery文件夹中的jquery-1.4.4.min.js
(上传文件域名字为:filedata
返回结构必需为json,并且结构如下:{"err":"","msg":"200906030521128703.gif"})

3.在相应html文件的</head>之前添加
<script src="/static/js/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="/static/xheditor/xheditor-1.1.14-zh-cn.min.js" type="text/javascript"></script>
4.在您的页面初始JS代码里加上:
<script type="text/javascript">
$(function(){
$('#txt_notes').xheditor({width:'800',height:'300',upImgUrl:"/admin/upload/",upImgExt:"jpg,jpeg,gif,png"});
})
</script>
Python&Django篇
先看下项目的结构图

1.settings.py配置
MIDDLEWARE_CLASSES中注释(否则出现403错误)
#'django.middleware.csrf.CsrfViewMiddleware', import os
MEDIA_ROOT = os.path.join(os.path.dirname(__file__),"travels").replace('\\','/')
2.urls.py 配置
url(r'^admin/upload/$','wetrip.views.upload.upload_image'),
url(r'^static/(?P<path>.*)$','django.views.static.serve',
{'document_root': "/目录/static/"}),
url(r'^pictures/(?P<path>.*)$','django.views.static.serve',
{'document_root': "/目录/travels/pictures"}),
3.上传主要代码
upload.py
#coding=utf-8
from django.http import HttpResponse
from django.utils import simplejson
import time,os
import datetime as dt
import wetrip.settings #允许上传文件类型
ALLOW_SUFFIX =['.jpg','.png','.jpeg','.gif']
#目录创建
def create_dir():
today = dt.datetime.today()
dir_name = '/pictures/%d/%d/%d' %(today.year,today.month,today.day)
if not os.path.exists(wetrip.settings.MEDIA_ROOT + dir_name):
os.makedirs(wetrip.settings.MEDIA_ROOT + dir_name)
return dir_name def upload_image(request):
dir_name = create_dir()
if 'HTTP_CONTENT_DISPOSITION' in request.META:#chrome/firefox Xheditor使用的是Html5方式上传
disposition = request.META['HTTP_CONTENT_DISPOSITION']
image_name_suffix = disposition[ disposition.rindex('.') : disposition.rindex('"') ]
data = request.body #request.raw_post_data#已过时
return write_data(data,image_name_suffix,dir_name,True)
else:#普通上传,ie
if 'filedata' in request.FILES:
image_name = request.FILES["filedata"].name
image_name_suffix = image_name[image_name.rindex('.') : ]
return write_data(request.FILES["filedata"],image_name_suffix,dir_name,False)
else:
return HttpResponse(simplejson.dumps({'err':'未选择文件','msg':''},ensure_ascii = False)) #保存图片
def write_data(data,image_name_suffix,dir_name,html5):
if image_name_suffix in ALLOW_SUFFIX:
image_name = str(int(time.time())) + image_name_suffix
try:
with open(wetrip.settings.MEDIA_ROOT + dir_name+'/'+ image_name,'wb') as destination:
if html5:
destination.write(data)#写文件流
else:
for c in data.chunks():
destination.write(c)
return HttpResponse(simplejson.dumps({'err':'','msg':dir_name+'/'+image_name},ensure_ascii = False))
except Exception,e:
return HttpResponse(simplejson.dumps({'err':e.message,'msg':''},ensure_ascii = False))
else:
return HttpResponse(simplejson.dumps({'err':'上传格式不准确!只支持jpg,png,jpeg,gif','msg':''},ensure_ascii = False))
[原创]Python/Django使用富文本编辑器XHeditor上传本地图片的更多相关文章
- Django使用富文本编辑器
1.下载kindeditor 网址:http://kindeditor.net/demo.php2.解压到项目中 地址:\static\js\kindeditor-4.1.103.删除没用的文件 例如 ...
- 给Django后台富文本编辑器添加上传文件的功能
使用富文本编辑器上传的文件是要放到服务器上的,所以这是一个request.既然是一个request,就需要urls.py进行转发请求views.py进行处理.views.py处理完了返回一个文件所在的 ...
- c# mvc使用富文本编辑器数据上传回显问题,图片,附件上传解决方案
1.首先去官网下载编辑器:http://ueditor.baidu.com/website/download.html 我用的是asp.net mvc开发模式所以选的是asp 2.前端页面必须引 ...
- Django使用富文本编辑器ckediter
1 - 安装 pip install django-ckeditor 2 - 注册APP ckeditor 3 - 由于djang-ckeditor在ckeditor-init.js文件中使用了JQu ...
- Django实现的博客系统中使用富文本编辑器ckeditor
操作系统为OS X 10.9.2,Django为1.6.5. 1.下载和安装 1.1 安装 ckeditor 下载地址 https://github.com/shaunsephton/django-c ...
- Django中使用富文本编辑器Uedit
Uedit是百度一款非常好用的富文本编辑器 一.安装及基本配置 官方GitHub(有详细的安装使用教程):https://github.com/zhangfisher/DjangoUeditor 1. ...
- Django后台管理admin或者adminx中使用富文本编辑器
在admin或者adminx后台中使用富文本编辑器 一.建立模型:(安装django-tinymce==2.6.0) from django.db import models from tinymce ...
- flask项目中使用富文本编辑器
flask是一个用python编写的轻量级web框架,基于Werkzeug WSGI(WSGI: python的服务器网关接口)工具箱和Jinja2模板,因为它使用简单的核心,用extension增加 ...
- django-应用中和amdin使用富文本编辑器kindeditor
文章描述.新闻详情和产品介绍等,都需要大量的文字描述信息或图片.视频.文字的编辑等,这个时候我们就需要介绍第三方富文本编辑器. 今天介绍的是django中绑定和应用kindeditor编辑器: 效果如 ...
随机推荐
- 【版本管理】自定义git
Git除了可配置user.name和user.email外,实际上,Git还有很多可配置项. 如 $ git config --global color.ui true,让Git显⽰示颜⾊色,会让命令 ...
- 一本通1656Combination
1656:Combination 时间限制: 1000 ms 内存限制: 524288 KB [题目描述] 原题来自:BZOJ 2982 LMZ 有 n 个不同的基友,他每天晚上要选 ...
- Hello 2019 自闭记
A:8min才过??? #include<iostream> #include<cstdio> #include<cmath> #include<cstdli ...
- Queries about less or equal elements CodeForces - 600B(二分)
You are given two arrays of integers a and b. For each element of the second arraybj you should find ...
- The Super Powers UVA - 11752(合数幂)
题意: 求1~2^64-1之间所有的 至少是两个不同的正整数的幂的数 升序输出 一个数的合数次幂即为这样的数 找出1~2^64-1中所有数的合数次幂 用set存起来(既能防止重复 又能升序) 最后输 ...
- MT【92】空间余弦定理解题
评:学校常规课堂教学里很少讲到这个,有点可惜.
- [Swerc2014 C]Golf Bot
题意:给你N个数字,每次利用这N个数字中最多两个数字进行加法运算,来得到目标中的M个数字. Solution: 我们先来看看多项式乘法:\(A(x)=\sum_{i=0}^{n-1}a_ix^i\), ...
- 【题解】 Codeforces 919F A Game With Numbers(拓扑排序+博弈论+哈希)
懒得复制,戳我戳我 Solution: 我感觉我也说不太好,看Awson的题解吧. 说一点之前打错的地方: 连边存的是hash后的数组下标 if(ans[ num( C[a.hash()] , C[b ...
- HDU 4280 Island Transport(网络流,最大流)
HDU 4280 Island Transport(网络流,最大流) Description In the vast waters far far away, there are many islan ...
- (转)java中使用memcache
背景:公司项目中使用java和memcache相结合来搭建缓存,所以要了解下缓存的基础知识! 1 了解memcache 1.1 基础知识 什么是Memcache? Memcache集群环境下缓存解决方 ...