django 发帖时碰到的图片上传

所用编辑器 【wangEditor.js】

图片上传接口 ‘/edit/image/‘ 返回内容  参照 https://www.kancloud.cn/wangfupeng/wangeditor3/335782

# 2018_12_29 日更新

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>wangEditor demo</title>
</head>
  <script type="text/javascript">
function modifyContent() {
var introduce = document.getElementById("introduce");
introduce.value = editor.txt.html();
}
</script>

<body>
<form action="" method="post" onsubmit="modifyContent()">
<div>
<!-- 编辑器编辑,提交时执行js,获得编辑器的内容,赋值给textarea,用于向后台提交存入数据库 -->
<textarea rows="5" cols="35" name="usIntroduce" style="display:none;" id="introduce"></textarea>
<div id="editor">
{#        <p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p>#}
        <p>欢迎使用 <b>wangEditor</b> 富文本编辑器</p><p><img src="/static/upload/cart.png" style="max-width:100%;"></p>
</div>
        <!-- 注意, 只需要引用 JS,无需引用任何 CSS !!!-->
        <script type="text/javascript" src="/static/wangEditor.js"></script>
        <!-- 设置全屏功能的两个js文件,先引入jquery-3.2.1.min.js,在引入wangEditor-fullscreen-plugin.js -->
       
        <script type="text/javascript">
            var E = window.wangEditor
            var editor = new E(‘#editor‘)
            /* 处理上传图片的controller路径 */
             editor.customConfig.uploadImgServer = ‘/edit/image/‘

                                                                                                        /* 定义上传图片的默认名字 */
             editor.customConfig.uploadFileName = ‘myFileName‘
            // 或者 var editor = new E( document.getElementById(‘editor‘) )
            editor.create()
            //初始化全屏插件
        </script>
</div> 
<input type="submit" value="提交"></input>
    {% csrf_token %}
</form>

</body>
</html>

---前端代码

from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from suibi1.settings import upload_img
import os
# Create your views here.
def index(request):
    if request.method==‘GET‘:
        return render(request,‘index.html‘)
    elif request.method=="POST":
        print(request.POST.get(‘usIntroduce‘))
        return HttpResponse(‘提交成功‘)

@csrf_exempt
def image(request):
    if request.method=="POST":
        a = request.FILES[‘myFileName‘]
        name = a.name
        with open(os.path.join(upload_img,name),‘wb‘) as f:
            f.write(a.file.read())
            print(‘图片写入成功‘)
            return JsonResponse({"errno": 0, "data":[os.path.join(‘/static/upload‘,name),]})

--django_views

原文地址:https://www.cnblogs.com/Skyda/p/10197497.html

时间: 2024-07-31 17:22:17

django 发帖时碰到的图片上传的相关文章

百度编辑器contentChange监听不到图片上传

将ueditor组件化到java项目中,当调用组件后,绑定函数,监听contentchange如下图: um.addListener("contentChange",function(){ var content=UE.getEditor('editor').getContent(); $("#content").html(content);}); 当内容变化时,获取变化复制到data中,没有问题,可是在单张图片上传时,在图片上传过程中调用了函数,图片上传成功后,没

layui图片上传之后后台如何修改图片的后缀名以及返回数据给前台

const pathLib = require('path');//引入node.js下的一个path模块的方法,主要处理文件的名字等工作,具体可看文档 const fs = require(''fs); var app = new express(); //前台图片上传访问路径 app.post('/upload',(req,res)=>{ if(Boolean(typeof req.files[0])){//判断访问该后台时是否有图片上传 var ext = pathLib.parse(re

Django配置图片上传

本文首先实现django中上传图片的过程,然后解决富文本编辑器文件上传的问题. 一. 上传图片 1.在 settings.py 中配置MEDIA_URL  和 MEDIA_ROOT 在 D:\blog_project  下建立文件夹 uploads MEDIA_URL = '/uploads/' # 上传图片的路径:D:\blog_project\uploads MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads') # 上传图片的根路径 BASE_DIR:

django之创建第10-1个项目-图片上传并记录上传时间

1.百度云盘:django之创建第10-1个项目-图片上传并记录上传时间 2.主要修改的配置文件有3个,forms.views和models3个文件以及html 3.forms.py文件修改 #coding:utf-8 #这里定义html页面中用到的表单,常和views和models文件配合使用 """ >>> help(django) Help on package django: PACKAGE CONTENTS conf (package) contr

kindeditor更改图片上传时网络图片的路径

当我们想要使用kindeditor的图片上传功能时,有两种选择图片方式,一种是本地选择,一种是在图片空间中选择,图片空间的默认地址是服务器上的/kindeditor/attached/image/下面. 如果想要改变这个路径,需要找到/kindeditor/php/file_manager_json.php这个文件,然后可以看到下面几行: //根目录路径,可以指定绝对路径,比如 /var/www/attached/ $root_path = $php_path . '../attached/';

使用Kindeditor的多文件(图片)上传时出现上传失败的解决办法/使用Flash上传多文件(图片)上传时上传失败的解决办法

近来用户反映希望我们把在线编辑器中的多图片上传功能实现,因为他们在编辑商品描述时经常会有一次上传多张图片的需求,如果要逐张选择的话效率很低,客户的需求就是我们的追求,很快我们就把完善功能排到了日程表中,要求尽快实现. 我们在项目中使用的在线编辑器是Kindeditor4.1.10,它们的多文件上传插件是使用Flash实现的,原本应该就是能使用的,但为什么老是显示上传失败的,百度了一下前人的经验和教训,出现这种情况,有两种可能:1)上传的目标文件夹没有写权限,导致上传的文件无法进行写操作,所以上传

图片上传时获取图片的宽和高

经常会遇到图片上传的问题,这时候我们会传图片的地址,宽和高到服务器,至于图片上传就不说了,这里主要说图片上传时获取图片的原始宽和高的问题. 一般而言,我们把图片上传至服务器时,服务器会返回一个上传地址给我们,这个就是我们图片的url了,但是光有这个还是不够的,因为还要将图片的宽和高传给服务器,这时候就可以这样做了.直接上代码: var img = new Image() img.src = url 然后就可以使用img.width和img.height来获取图片的宽和高了.当然仅仅这样做是不够的

django admin后台接入tinymce并且支持图片上传

首先:下载tinymce 地址是https://www.tinymce.com/ 点击download 下载社区版本即可 接着:把压缩包内tinymce目录内的所有文件和文件夹复制到Django项目中static/js目录内: 然后编辑admin文件 class BlogAdmin(admin.ModelAdmin): list_display = ("title", "author", "category", "public_time

Django(十九)文件上传:图片上传(后台上传、自定义上传)、

一.基本设置 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/http/file-uploads/ 1)配置project1/settings.py 因为图片也属于静态文件,所以保存到static目录下. MEDIA_ROOT=os.path.join(BASE_DIR,"static/media") 2)在static目录下创建media目录,再创建应用名称的目录,此例为app1 F:\Test\django-demo\pro