Vue.js中使用wangEditor富文本编辑器

1.前端代码

前端HTML




欢迎使用 wangEditor 富文本编辑器

添加

前端js

2.后端代码(python + Django)

django路由

from django.conf.urls import patterns

from home_application import host_view

urlpatterns = patterns(
    'home_application.views',
    (r'^$', 'home'),
    (r'^api/test/$', "test"),
    (r'^upload_img/$', host_view.upload_img),
    (r'^media/(?P\d+).(?P\w+)', host_view.get_media),
    ...
)

django视图

import os
import time

from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse, HttpResponse
from django.utils.encoding import escape_uri_path

def check_upload_wrapper(func):
    def inner(*args, **kwargs):
        if not os.path.exists("media/"):
            os.makedirs("media/")
        return func(*args, **kwargs)
    return inner

def create_blog(request):
    data = json.loads(request.body)
    content = data.get("content")
    print(content)
    return JsonResponse({"result": True})

def get_media(request, name, postfix):
    file_name = name + "." + postfix
    file_path = os.path.join("media", file_name)
    file = open(file_path, 'rb')
    response = HttpResponse(file)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = "attachment;filename*=utf-8''{}".format(escape_uri_path(file_name))
    return response

@csrf_exempt
@check_upload_wrapper
def upload_img(request):
    file_list = []

    for k, v in request.FILES.items():
        t = time.strftime('%Y%m%d%H%M%S')
        now_file_name = t + '.' + k.split('.')[-1]
        file_path = os.path.join('media', now_file_name)

        with open(file_path, "ab") as f:
            f.write(v.read())
        file_list.append("/" + file_path)

    return JsonResponse({"errno": 0, "data": file_list})

你可能感兴趣的:(Vue.js中使用wangEditor富文本编辑器)