加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

KindEditor编辑器

发布时间:2020-12-16 23:56:57 所属栏目:Python 来源:网络整理
导读:1.官网:http://kindeditor.net/doc.php 2.下载:http://kindeditor.net/down.php 3.目录说明 | -all- - - 4.开始使用 span style="color: #0000ff;" span style="color: #800000;"script span style="color: #ff0000;"src span style="color: #0000ff;"="/s

1.官网:http://kindeditor.net/doc.php

2.下载:http://kindeditor.net/down.php

3.目录说明

|-all---

4.开始使用

<span style="color: #0000ff;"><<span style="color: #800000;">script <span style="color: #ff0000;">src<span style="color: #0000ff;">="/static/jquery-1.12.4.js"<span style="color: #0000ff;">></<span style="color: #800000;">script<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">script <span style="color: #ff0000;">src<span style="color: #0000ff;">="/static/plugins/kind-editor/kindeditor-all.js"<span style="color: #0000ff;">></<span style="color: #800000;">script<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">script<span style="color: #0000ff;">><span style="background-color: #f5f5f5; color: #000000;">
$(<span style="background-color: #f5f5f5; color: #0000ff;">function<span style="background-color: #f5f5f5; color: #000000;"> () {
initKindEditor();
});
<span style="background-color: #f5f5f5; color: #0000ff;">function<span style="background-color: #f5f5f5; color: #000000;"> initKindEditor() {
<span style="background-color: #f5f5f5; color: #0000ff;">var<span style="background-color: #f5f5f5; color: #000000;"> kind <span style="background-color: #f5f5f5; color: #000000;">=<span style="background-color: #f5f5f5; color: #000000;"> KindEditor.create(<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">#content<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">,{
width: <span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">100%<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">,<span style="background-color: #f5f5f5; color: #008000;">//<span style="background-color: #f5f5f5; color: #008000;"> 文本框宽度(可以百分比或像素)
<span style="background-color: #f5f5f5; color: #000000;"> height: <span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">300px<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">,<span style="background-color: #f5f5f5; color: #008000;">//<span style="background-color: #f5f5f5; color: #008000;"> 文本框高度(只能像素)
<span style="background-color: #f5f5f5; color: #000000;"> minWidth: <span style="background-color: #f5f5f5; color: #000000;">200<span style="background-color: #f5f5f5; color: #000000;">,<span style="background-color: #f5f5f5; color: #008000;">//<span style="background-color: #f5f5f5; color: #008000;"> 最小宽度(数字)
<span style="background-color: #f5f5f5; color: #000000;"> minHeight: <span style="background-color: #f5f5f5; color: #000000;">400 <span style="background-color: #f5f5f5; color: #008000;">//<span style="background-color: #f5f5f5; color: #008000;"> 最小高度(数字)
<span style="background-color: #f5f5f5; color: #000000;"> });
}
<span style="color: #0000ff;"></<span style="color: #800000;">script<span style="color: #0000ff;">>

5.初始化参数:http://kindeditor.net/docs/option.html

6.上传文件

默认上传地址:js文件路径+/php/upload_json.php

修改初始参数uploadJson改变上传url:uploadJson:'/upload/'

默认本地上传文件名:imgFile,可通过filePostName设置

服务端返回数据格式:

dic =: 0, : , :

示例:

<span style="color: #0000ff;"><<span style="color: #800000;">div<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">h1<span style="color: #0000ff;">>文章内容<span style="color: #0000ff;"></<span style="color: #800000;">h1<span style="color: #0000ff;">><span style="color: #000000;">
{{ request.POST.content|safe }}
<span style="color: #0000ff;"></<span style="color: #800000;">div<span style="color: #0000ff;">>

<span style="color: #0000ff;"><<span style="color: #800000;">form <span style="color: #ff0000;">method<span style="color: #0000ff;">="POST"<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">h1<span style="color: #0000ff;">>请输入内容:<span style="color: #0000ff;"></<span style="color: #800000;">h1<span style="color: #0000ff;">><span style="color: #000000;">
{% csrf_token %}
<span style="color: #0000ff;"><<span style="color: #800000;">div <span style="color: #ff0000;">style<span style="color: #0000ff;">="width: 500px; margin: 0 auto;"<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">textarea <span style="color: #ff0000;">name<span style="color: #0000ff;">="content"<span style="color: #ff0000;"> id<span style="color: #0000ff;">="content"<span style="color: #0000ff;">></<span style="color: #800000;">textarea<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">div<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">input <span style="color: #ff0000;">type<span style="color: #0000ff;">="submit"<span style="color: #ff0000;"> value<span style="color: #0000ff;">="提交"<span style="color: #0000ff;">/>
<span style="color: #0000ff;"></<span style="color: #800000;">form<span style="color: #0000ff;">>

<span style="color: #0000ff;"><<span style="color: #800000;">script <span style="color: #ff0000;">src<span style="color: #0000ff;">="/static/jquery-1.12.4.js"<span style="color: #0000ff;">></<span style="color: #800000;">script<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">script <span style="color: #ff0000;">src<span style="color: #0000ff;">="/static/plugins/kind-editor/kindeditor-all.js"<span style="color: #0000ff;">></<span style="color: #800000;">script<span style="color: #0000ff;">>
<span style="color: #0000ff;"><<span style="color: #800000;">script<span style="color: #0000ff;">><span style="background-color: #f5f5f5; color: #000000;">
$(<span style="background-color: #f5f5f5; color: #0000ff;">function<span style="background-color: #f5f5f5; color: #000000;"> () {
initKindEditor();
});

</span><span style="background-color: #f5f5f5; color: #0000ff;"&gt;function</span><span style="background-color: #f5f5f5; color: #000000;"&gt; initKindEditor() {
    </span><span style="background-color: #f5f5f5; color: #0000ff;"&gt;var</span><span style="background-color: #f5f5f5; color: #000000;"&gt; a </span><span style="background-color: #f5f5f5; color: #000000;"&gt;=</span> <span style="background-color: #f5f5f5; color: #000000;"&gt;'</span><span style="background-color: #f5f5f5; color: #000000;"&gt;kind</span><span style="background-color: #f5f5f5; color: #000000;"&gt;'</span><span style="background-color: #f5f5f5; color: #000000;"&gt;;
    </span><span style="background-color: #f5f5f5; color: #0000ff;"&gt;var</span><span style="background-color: #f5f5f5; color: #000000;"&gt; kind </span><span style="background-color: #f5f5f5; color: #000000;"&gt;=</span><span style="background-color: #f5f5f5; color: #000000;"&gt; KindEditor.create(</span><span style="background-color: #f5f5f5; color: #000000;"&gt;'</span><span style="background-color: #f5f5f5; color: #000000;"&gt;#content</span><span style="background-color: #f5f5f5; color: #000000;"&gt;'</span><span style="background-color: #f5f5f5; color: #000000;"&gt;,</span><span style="background-color: #f5f5f5; color: #008000;"&gt;//</span><span style="background-color: #f5f5f5; color: #008000;"&gt; 最小宽度(数字)</span>

<span style="background-color: #f5f5f5; color: #000000;"> minHeight: <span style="background-color: #f5f5f5; color: #000000;">400<span style="background-color: #f5f5f5; color: #000000;">,<span style="background-color: #f5f5f5; color: #008000;">//<span style="background-color: #f5f5f5; color: #008000;"> 最小高度(数字)
<span style="background-color: #f5f5f5; color: #000000;"> uploadJson: <span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">/kind/upload_img/<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">,extraFileUploadParams: {
<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">csrfmiddlewaretoken<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">: <span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">{{ csrf_token }}<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">
},fileManagerJson: <span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">/kind/file_manager/<span style="background-color: #f5f5f5; color: #000000;">'<span style="background-color: #f5f5f5; color: #000000;">,allowPreviewEmoticons: <span style="background-color: #f5f5f5; color: #0000ff;">true<span style="background-color: #f5f5f5; color: #000000;">,allowImageUpload: <span style="background-color: #f5f5f5; color: #0000ff;">true<span style="background-color: #f5f5f5; color: #000000;">
});
}
<span style="color: #0000ff;"></<span style="color: #800000;">script<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">body<span style="color: #0000ff;">>
<span style="color: #0000ff;"></<span style="color: #800000;">html<span style="color: #0000ff;">>

服务端处理:

<span style="color: #0000ff;">from django.shortcuts <span style="color: #0000ff;">import<span style="color: #000000;"> render
<span style="color: #0000ff;">from
django.shortcuts <span style="color: #0000ff;">import
<span style="color: #000000;"> HttpResponse

<span style="color: #0000ff;">def<span style="color: #000000;"> index(request):
<span style="color: #800000;">"""<span style="color: #800000;">
首页
:param request:
:return:
<span style="color: #800000;">"""
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">'<span style="color: #800000;">index.html<span style="color: #800000;">'<span style="color: #000000;">)

<span style="color: #0000ff;">def<span style="color: #000000;"> upload_img(request):
<span style="color: #800000;">"""<span style="color: #800000;">
文件上传
:param request:
:return:
<span style="color: #800000;">"""<span style="color: #000000;">
dic =<span style="color: #000000;"> {
<span style="color: #800000;">'<span style="color: #800000;">error<span style="color: #800000;">'<span style="color: #000000;">: 0,<span style="color: #800000;">'<span style="color: #800000;">url<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">/static/imgs/20130809170025.png<span style="color: #800000;">'<span style="color: #000000;">,<span style="color: #800000;">'<span style="color: #800000;">message<span style="color: #800000;">': <span style="color: #800000;">'<span style="color: #800000;">错误了...<span style="color: #800000;">'<span style="color: #000000;">
}

</span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; HttpResponse(json.dumps(dic))

<span style="color: #0000ff;">def<span style="color: #000000;"> file_manager(request):
<span style="color: #800000;">"""<span style="color: #800000;">
文件管理
:param request:
:return:
<span style="color: #800000;">"""<span style="color: #000000;">
dic =<span style="color: #000000;"> {}
root_path = <span style="color: #800000;">'<span style="color: #800000;">/editors/static/<span style="color: #800000;">'<span style="color: #000000;">
static_root_path = <span style="color: #800000;">'<span style="color: #800000;">/static/<span style="color: #800000;">'<span style="color: #000000;">
request_path = request.GET.get(<span style="color: #800000;">'<span style="color: #800000;">path<span style="color: #800000;">'<span style="color: #000000;">)
<span style="color: #0000ff;">if<span style="color: #000000;"> request_path:
abs_current_dir_path =<span style="color: #000000;"> os.path.join(root_path,request_path)
move_up_dir_path = os.path.dirname(request_path.rstrip(<span style="color: #800000;">'<span style="color: #800000;">/<span style="color: #800000;">'<span style="color: #000000;">))
dic[<span style="color: #800000;">'<span style="color: #800000;">moveup_dir_path<span style="color: #800000;">'] = move_up_dir_path + <span style="color: #800000;">'<span style="color: #800000;">/<span style="color: #800000;">' <span style="color: #0000ff;">if move_up_dir_path <span style="color: #0000ff;">else<span style="color: #000000;"> move_up_dir_path

</span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;:
    abs_current_dir_path </span>=<span style="color: #000000;"&gt; root_path
    dic[</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;moveup_dir_path</span><span style="color: #800000;"&gt;'</span>] = <span style="color: #800000;"&gt;''</span><span style="color: #000000;"&gt;

dic[</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;current_dir_path</span><span style="color: #800000;"&gt;'</span>] =<span style="color: #000000;"&gt; request_path
dic[</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;current_url</span><span style="color: #800000;"&gt;'</span>] =<span style="color: #000000;"&gt; os.path.join(static_root_path,request_path)

file_list </span>=<span style="color: #000000;"&gt; []
</span><span style="color: #0000ff;"&gt;for</span> item <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; os.listdir(abs_current_dir_path):
    abs_item_path </span>=<span style="color: #000000;"&gt; os.path.join(abs_current_dir_path,item)
    a,exts </span>=<span style="color: #000000;"&gt; os.path.splitext(item)
    is_dir </span>=<span style="color: #000000;"&gt; os.path.isdir(abs_item_path)
    </span><span style="color: #0000ff;"&gt;if</span><span style="color: #000000;"&gt; is_dir:
        temp </span>=<span style="color: #000000;"&gt; {
            </span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;is_dir</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: True,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;has_file</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: True,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;filesize</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: 0,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;dir_path</span><span style="color: #800000;"&gt;'</span>: <span style="color: #800000;"&gt;''</span><span style="color: #000000;"&gt;,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;is_photo</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: False,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;filetype</span><span style="color: #800000;"&gt;'</span>: <span style="color: #800000;"&gt;''</span><span style="color: #000000;"&gt;,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;filename</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: item,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;datetime</span><span style="color: #800000;"&gt;'</span>: time.strftime(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;%Y-%m-%d %H:%M:%S</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;,time.gmtime(os.path.getctime(abs_item_path)))
        }
    </span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;:
        temp </span>=<span style="color: #000000;"&gt; {
            </span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;is_dir</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: False,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;has_file</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: False,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;filesize</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;: os.stat(abs_item_path).st_size,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;is_photo</span><span style="color: #800000;"&gt;'</span>: True <span style="color: #0000ff;"&gt;if</span> exts.lower() <span style="color: #0000ff;"&gt;in</span> [<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;.jpg</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;.png</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;.jpeg</span><span style="color: #800000;"&gt;'</span>] <span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt; False,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;filetype</span><span style="color: #800000;"&gt;'</span>: exts.lower().strip(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;.</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;),time.gmtime(os.path.getctime(abs_item_path)))
        }

    file_list.append(temp)
dic[</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;file_list</span><span style="color: #800000;"&gt;'</span>] =<span style="color: #000000;"&gt; file_list
</span><span style="color: #0000ff;"&gt;return</span> HttpResponse(json.dumps(dic))</pre>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读