Loading

在DjangoAdmin中使用KindEditor(上传图片)

一、下载

http://kindeditor.net/down.php

删除asp、asp.net、php、jsp、examples文件夹

拷贝到static目录下

二、配置

kindeditor目录下新建conifg.js

KindEditor.ready(function(K) {
    K.create('#id_content', {
        width: '800px',
        height: '500px',
        uploadJson:'/upload_image',
    });
    //http://kindeditor.net/docs/option.html
});

 

 admin.py

class NoticeAdmin(admin.ModelAdmin):
    class Media:
        js = [
            '/static/plugins/kindeditor/kindeditor-all-min.js',
            '/static/plugins/kindeditor/lang/zh-CN.js',
            '/static/plugins/kindeditor/config.js'
        ]
admin.site.register(Notice, NoticeAdmin)

 views.py

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def upload_image(request):
    allow_suffix = ['jpg', 'png', 'jpeg', 'gif', 'bmp']
    files = request.FILES.get("imgFile", None)
    if files:
        file_suffix = files.name.split(".")[-1]
        if file_suffix not in allow_suffix:
            return JsonResponse({"error": 1, "message": "图片格式不正确"})
        file_name = str(uuid.uuid1()) + "." + file_suffix
        file_path = os.path.join(settings.MEDIA_ROOT, 'image', file_name)
        url_path = os.path.join(settings.MEDIA_URL, 'image', file_name)
        print file_path
        with open(file_path, 'wb') as f:
            f.write(files.file.read())
        return JsonResponse({"error": 0, "url": url_path})

 settings.py

MEDIA_URL = "/upload/"
MEDIA_ROOT = os.path.join(BASE_DIR, "upload")

 urls.py

from django.views.static import serve
url(r'^upload/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT,}),

 

posted @ 2019-05-06 16:53  LB477  阅读(508)  评论(0编辑  收藏  举报