简体   繁体   English

Trumbowyg:Django服务器可以检测文件上传但不能检测图像URL输入

[英]Trumbowyg: Django server can detect file upload but not image URL input

I'm using Trumbowyg , a WYSIWYG JavaScript editor which has a feature of rendering images from URLs pasted in. It also has an upload plugin which enables uploading local images and custom server side handling. 我正在使用Trumbowyg ,一个WYSIWYG JavaScript编辑器,它具有从粘贴的URL渲染图像的功能。它还有一个上传插件 ,可以上传本地图像和自定义服务器端处理。

My python/django function upload_image() can successfully detect the uploaded image - however when I use the URL image input, my python function cannot detect it. 我的python / django函数upload_image()可以成功检测上传的图像 - 但是当我使用URL图像输入时,我的python函数无法检测到它。 Trumbowyg simply renders the image without going through my python backend. Trumbowyg只是渲染图像而不通过我的python后端。

Here's my code: 这是我的代码:

$('#id_content').trumbowyg({
    btnsDef: {
        // Create a new dropdown
        image: {
            dropdown: ['insertImage', 'upload'],
            ico: 'insertImage'
        }
    },
    // Redefine the button pane
    btns: [
        ['strong', 'em', 'del'],
        ['link'],
        ['image'], // Our fresh created dropdown
    ],
    plugins: {
        // Add imagur parameters to upload plugin for demo purposes
        upload: {
            serverPath: '/upload_image/',
            fileFieldName: 'content_image',
            urlPropertyName: 'url'
        }
    }
});
def upload_image(request):
    print('Success') #only prints when I use the upload input, not the URL input

Why can in detect the uploaded image but not the URL input? 为什么可以检测上传的图像而不是URL输入?

As already pointed, Trumbowyg doesn't send the URL to backend if the user uploads the image using a URL. 如前所述,如果用户使用URL上传图像,Trumbowyg不会将URL发送到后端。

But if you really want to host the images on your own server, there's a way you can do that. 但是,如果您真的想在自己的服务器上托管图像,那么有一种方法可以做到这一点。

When the user submits the form, you'll receive the content of the textarea in your backend. 当用户提交表单时,您将在后端收到textarea的内容。 You can then read the content and look for <img src="..."> tag. 然后,您可以阅读内容并查找<img src="...">标记。

At that point, you can check if the src value doesn't start with your S3 bucket hostname, you can download that image using urllib or requests library, save it to your bucket and replace the src value. 此时,您可以检查src值是否不以S3存储桶主机名开头,您可以使用urllibrequests库下载该映像,将其保存到存储桶并替换src值。

Since the submitted data will be in HTML format, check out the excellent Beautiful Soup . 由于提交的数据将采用HTML格式,因此请查看优秀的Beautiful Soup It will make parsing HTML easy. 它将使解析HTML变得容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM