标签:
1,把kindeditor的包放在了/static/目录下,然后引入下面的几个包(使用的最简模式)
<link href="{% static ‘kindeditor/themes/default/default.css‘ %}" rel="stylesheet">
<script src="{% static ‘kindeditor/kindeditor-all-min.js‘ %}"></script>
<script src="{% static ‘kindeditor/lang/zh-CN.js‘ %}"></script>
2,js代码
var editor;
KindEditor.ready(function(K) {
editor = K.create(‘textarea[name="content"]‘, {
resizeType : 1,
allowPreviewEmoticons : false,
<!--去掉远程上传的功能-->
allowImageRemote : false,
<!--后台处理上传图片的功url-->
uploadJson : ‘/main/uploadImg/‘,
items : [
‘fontname‘, ‘fontsize‘, ‘|‘, ‘forecolor‘, ‘hilitecolor‘, ‘bold‘, ‘italic‘, ‘underline‘,
‘removeformat‘, ‘|‘, ‘justifyleft‘, ‘justifycenter‘, ‘justifyright‘, ‘insertorderedlist‘,
‘insertunorderedlist‘, ‘|‘, ‘emoticons‘, ‘image‘, ‘link‘]
});
})
3,后台代码
@csrf_exempt
def uploadImg(request):
if request.method == ‘POST‘:
file_obj = open("log.txt","w+")
buf = request.FILES.get(‘imgFile‘, None)
print >> file_obj,str(buf)
file_buff = buf.read()
time_format=str(time.strftime("%Y-%m-%d-%H%M%S",time.localtime()))
try:
file_name = "img_"+time_format+".jpg"
save_file("main/static/content_img", file_name, file_buff)
dict_tmp = {}
dict_tmp["error"] = 0
dict_tmp["url"] = "/static/content_img/"+file_name
return HttpResponse(json.dumps(dict_tmp))
except Exception,e:
dict_tmp = {}
dict_tmp["error"] = 1
print >> file_obj,e
return HttpResponse(json.dumps(dict_tmp))
#对path进行处理
def mkdir(path):
# 去除左右两边的空格
path=path.strip()
# 去除尾部 \符号
path=path.rstrip("\\")
if not os.path.exists(path):
os.makedirs(path)
return path
#保存相关的文件
def save_file(path, file_name, data):
if data == None:
return
mkdir(path)
if(not path.endswith("/")):
path=path+"/"
file=open(path+file_name, "wb")
file.write(data)
file.flush()
file.close()
4,注意,后台成功和失败后返回的json格式和字段,否则上传成功,但是前台没反映。这个的效果是前台上传完毕,立即显示在kindeditor的编辑框里,返回的url就是让kindeditor找到这个图片
标签:
原文地址:http://my.oschina.net/yangting880815/blog/480875