Django配置-KindEditor

KindEditor

一、文件夹说明

├── asp                          asp示例,删掉
├── asp.net                    asp.net示例,删掉
├── attached                  空文件夹,放置关联文件attached
├── examples                 HTML示例,删掉
├── jsp                          java示例,删掉
├── kindeditor-all-min.js 全部JS(压缩)
├── kindeditor-all.js        全部JS(未压缩)
├── kindeditor-min.js      仅KindEditor JS(压缩)
├── kindeditor.js            仅KindEditor JS(未压缩)
├── lang                        支持语言
├── license.txt               License
├── php                        PHP示例,删掉
├── plugins                    KindEditor内部使用的插件
└── themes                   KindEditor主题

二、实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 配置文件:
# 与用户上传相关的配置
MEDIA_ROOT = os.path.join(BASE_DIR, "media" )
MEDIA_URL = "/media/"
 
# 路由中:
from django.urls import path, re_path
from django.views.static import serve
from django.conf import settings
from app01 import views
 
urlpatterns = [
     # media配置:
     re_path(r "media/(?P<path>.*)$" , serve, { "document_root" : settings.MEDIA_ROOT}),
     # 文本编辑器上传图片url
     path( 'ke_upload/' , views.ke_upload),
]
 
# 模板中:
"""
<div class="form-group">
     <label for="content">内容 </label>  <!--<textarea name="content" cols="30" rows="10"></textarea>-->
     {{ form.content }} <span>{{ form.content.errors.0 }}</span>
</div>
 
{% block js %}
<script src = "/static/js/jquery-3.2.1.min.js"> </script>
<script charset="utf-8" src="/static/plugins/kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="/static/plugins/kindeditor/lang/zh_CN.js"></script>
<script>
     // KindEditor 上传的瞬间,帮你生成iframe+form进行伪Ajax操作
     KindEditor.ready(function (K) {
         window.editor = K.create('textarea[name="content"]', {
                 resizeType: 1,
                 allowPreviewEmoticons: true,
                 allowImageUpload: true,
                 items: [
                     'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                     'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                     'insertunorderedlist', '|', 'emoticons', 'image', 'link'],
                 uploadJson: '/ke_upload/',
                 //filePostName:"upload_img",   //默认是imgFile
                 extraFileUploadParams:{
                     "csrfmiddlewaretoken":"{{ csrf_token }}"
                 }
 
         });
     });
</script>
{% endblock %}
 
"""
 
# 视图中:
from django.shortcuts import render, redirect, HttpResponse
import os
import time
import json
 
 
def ke_upload(request):
     file_obj = request.FILES.get( 'imgFile' # 通过filePostName修改
     # 上传后面的参数kd用的是dir
     # upload_img.html?dir=image  说明是图片
     # upload_img.html?dir=media  说明是视频,
     file_type = request.GET.get( 'dir' )
     file_obj.name = "%s%s" % (time.time(), file_obj.name)
     file_dir = 'media%s%s' % (os.sep, file_type)
     if not os.path.exists(file_dir):
         os.makedirs(file_dir)
     file_path = os.path.join( "media" , file_type, file_obj.name)
     with open (file_path, 'wb' ) as f:
         for line in file_obj:
             f.write(line)
 
     dic = {
         'error' : 0 ,
         'url' : '/media/%s/%s' % (file_type, file_obj.name),
         'message' : '错误了...'
     }
     return HttpResponse(json.dumps(dic))

三、补充

用kindeditor上传的flash和视频播放不了,打开编辑器的源码把type属性去掉就可以播放了

转载于:https://www.cnblogs.com/bubu99/p/10317775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值