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

时间:2022-09-03 21:21:34

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,一个所见即所得的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. Trumbowyg simply renders the image without going through my python backend.

我的python / django函数upload_image()可以成功检测上传的图像 - 但是当我使用URL图像输入时,我的python函数无法检测到它。 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输入?

1 个解决方案

#1


4  

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. You can then read the content and look for <img src="..."> tag.

当用户提交表单时,您将在后端收到textarea的内容。然后,您可以阅读内容并查找Trumbowyg:Django服务器可以检测文件上传但不能检测图像URL输入标记。

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存储桶主机名开头,您可以使用urllib或请求库下载该映像,将其保存到存储桶并替换src值。

Since the submitted data will be in HTML format, check out the excellent Beautiful Soup. It will make parsing HTML easy.

由于提交的数据将采用HTML格式,因此请查看优秀的Beautiful Soup。它将使解析HTML变得容易。

#1


4  

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. You can then read the content and look for <img src="..."> tag.

当用户提交表单时,您将在后端收到textarea的内容。然后,您可以阅读内容并查找Trumbowyg:Django服务器可以检测文件上传但不能检测图像URL输入标记。

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存储桶主机名开头,您可以使用urllib或请求库下载该映像,将其保存到存储桶并替换src值。

Since the submitted data will be in HTML format, check out the excellent Beautiful Soup. It will make parsing HTML easy.

由于提交的数据将采用HTML格式,因此请查看优秀的Beautiful Soup。它将使解析HTML变得容易。