• 在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,}),
    
  • 相关阅读:
    web测试学习大纲
    Python语言编程基础
    python文件IO
    Python 正则表达式
    python官网导航翻译
    python常用库
    python连接数据库
    sublime与python交互
    selenium连接浏览器方式
    sublime中运行python时编码格式问题
  • 原文地址:https://www.cnblogs.com/lb477/p/10820628.html
Copyright © 2020-2023  润新知