1.文本内容:这是渲染数据部分
<div id="editor"> {{ field }} </div>
2.这是JS内容:让textarea转换为编辑器
var WIKI_UPLOAD_URL = "{% url ‘web:wiki_upload‘ project_id=request.tracer.project.id %}" /* 初始化markdown编辑器(textarea转换为编辑器) */ function initEditorMd() { editormd(‘editor‘,{ placeholder: "请输入内容", height: 500, path: "{% static ‘/web/plugin/editor.md/lib/‘ %}", //path是告诉markdown组件的位置,需要的依赖 flowChart : true ,//支持流程图 imageUpload: true,//增加本地上传图片 imageFormats: ["jpg",‘png‘,‘jpeg‘,‘pdf‘,‘gif‘],//上传图片格式 imageUploadURL:WIKI_UPLOAD_URL,//上传到那个地址 }) }
3.视图内容:xframe_options_sameorigin装饰器是解决xframe_options报错的
from django.shortcuts import redirect,render,HttpResponse from django.urls import reverse #反向生成url from web.forms.wiki import WikiModelForm from web import models from django.http import JsonResponse from django.core import serializers from django.views.decorators.csrf import csrf_exempt from utils.tencent.cos import upload_file from django.views.decorators.clickjacking import xframe_options_sameorigin @xframe_options_sameorigin @csrf_exempt def wiki_upload(request,project_id): """markdown上传图片""" # result是markdown要求返回的数据格式 result = { ‘success‘: 0,#1成功,0失败 ‘message‘: None, ‘url‘: None } image_object = request.FILES.get("editormd-image-file") #如果没有获取到文件(失败) if not image_object: result[‘message‘] = "文件不存在" return JsonResponse(result) #image_object文件对象上传到当前项目的存储桶中 image_url = upload_file( request.tracer.project.bucket, request.tracer.project.region, image_object, image_object.name, ) #获取到文件对象image_object内容,改变其默认值(成功) result[‘success‘] = 1 result[‘url‘] = image_url return JsonResponse(result)
最终效果显示: