码迷,mamicode.com
首页 > Web开发 > 详细

kindeditor 图片上传

时间:2018-05-03 16:33:17      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:kindeditor 图片上传

视图函数

CONTENT = ""
from app01.forms import ArticleForm
def wangzhe(request):
    if request.method == "GET":
        obj = ArticleForm()
        return render(request,‘wangzhe.html‘,{‘obj‘:obj})
    else:
        obj = ArticleForm(request.POST)
        if obj.is_valid():
            content = obj.cleaned_data[‘content‘]
            global CONTENT
            CONTENT = content
            print(content)
            return HttpResponse(‘...‘)

def see(request):

    return render(request,‘see.html‘,{‘con‘: CONTENT})

def upload_img(request):
    import os
    upload_type = request.GET.get(‘dir‘)
    file_obj = request.FILES.get(‘imgFile‘)
    file_path = os.path.join(‘static/imgs‘,file_obj.name)
    with open(file_path,‘wb‘) as f:
        for chunk in file_obj.chunks():
            f.write(chunk)

    dic = {
        ‘error‘: 0,
        ‘url‘: ‘/‘ + file_path,
        ‘message‘: ‘错误了...‘
    }
    import json
    return HttpResponse(json.dumps(dic))

前端页面

<body>
    <form method="POST" action="/wangzhe.html" novalidate>
        {% csrf_token %}
        <p>
            文章标题
            {{ obj.title }}
        </p>

        <div>
            <div>文章内容</div>
            <div>
                {{ obj.content }}
            </div>
        </div>
        <input type="submit" value="提交" />
    </form>
    <script src="/static/kindeditor-4.1.10/kindeditor-all.js"></script>

    <script>
        KindEditor.create("#i1",{
            width: "700px",
            height: "300px",
            resizeType:1,
            uploadJson: ‘/upload_img.html‘,
            extraFileUploadParams:{
                "csrfmiddlewaretoken":"{{ csrf_token }}"
            }
        })
    </script>

</body>

kindeditor 图片上传

标签:kindeditor 图片上传

原文地址:http://blog.51cto.com/sgk2011/2112175

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!