• 在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', {
             '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,}),
    
  • 相关阅读:
    【转】js 获取浏览器高度和宽度值(多浏览器)
    Css相册
    微信公众号开发笔记2-自定义菜单
    微信公众号开发笔记1-获取Access Token
    【转】CSS选择器笔记
    【转】CSS浮动(float,clear)通俗讲解
    高云的jQuery源码分析笔记
    经典闭包例子详解
    执行控制——节流模式
    图片上下左右的无缝滚动的实现
  • 原文地址:https://www.cnblogs.com/lb477/p/10820628.html
Copyright © 2020-2023  润新知