kindeditor简单使用(Django)

富文本编辑框:
编辑框是一个插件,有多种,这里使用kind editor

一、从官网下载插件

  - http://kindeditor.net/doc.php

  - Django下插件中的有些目录无需使用,可以删除

    - asp - ASP程序

    - asp.net - ASP.NET程序

    - php - PHP程序

    - jsp - JSP程序

    - examples - 演示文件

二、导入插件

  - <script src="/static/kindeditor/kindeditor-all.js"></script>

三、生成富文本编辑框

  - 初始化参数有很多,详见  - http://kindeditor.net/docs/option.html#filemanagerjson

  - items参数默认全部

 1 <script>
 2     //整个页面加载完成后执行函数
 3     KindEditor.ready(function (K) {
 4         window.editor = K.create(
 5             "#editor",
 6             //编辑器初始化参数
 7             {
 8                 //高度和宽度有最小值650*100
 9 {#                width:'1000px',#}
10 {#                height:'800px',#}
11                 //配置编辑器的工具栏,其中”/”表示换行,”|”表示分隔符。
12                 items:[
13                     'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
14                     'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
15                     'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
16                     'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
17                     'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
18                     'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
19                     'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
20                     'anchor', 'link', 'unlink', '|', 'about',
21                 ],
22                 // 指定处理上传的url
23                 uploadJson:'../upload_img/',
24             }
25         )
26     })
27 </script>

四、对于上传文件的处理

  - 先生成一个文本标签 - <textarea name="" id="editor" cols="30" rows="10"></textarea>

  - 以图片上传为例,将图片保存到服务器端,并在文本框中预览

  - 要注意预览图片时返回的url的间隔符   " / "

 1 def upload_img(request):
 2     """
 3     上传文件的保存与状态信息的返回
 4     :param request:  
 5     :return: 返回状态,告诉前端是否上传成功
 6     """
 7     print(request.POST)
 8     print(request.FILES)
 9     print(BASE_DIR)
10     dic = {"error": None, "url": None, "message": None}
11     try:
12         # 根据文件名组成保存路径
13         file_name = os.path.join(BASE_DIR,"static","img",str(request.FILES.get("imgFile")))
14         print(file_name)
15         f = open( file_name,"wb")
16         for i in request.FILES.get("imgFile").chunks():   # 图片是一个对象,要调用chunks()方法一部分一部分的取
17             f.write(i)
18         f.close()
19         dic["error"] = 0
20         # 文件预览路径,必须使用 " / " ,os.path.join生成的文件路径无法预览
21         dic["url"] = "/static/img/" + str(request.FILES.get("imgFile"))
22     except Exception as e:
23         dic["error"] = 1
24         dic["message"] = e
25     """
26     //返回信息
27     {
28             "error" : 0,   # 0代表成功,1代表错误
29             "url" : "http://www.example.com/path/to/file.ext"  #图片预览
30             "message":"错误信息"
31     }
32     """
33     return JsonResponse(dic)
View Code

 

posted @ 2019-11-01 09:08  pywhy  阅读(235)  评论(0编辑  收藏  举报