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,一個所見即所得的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的內容。然后,您可以閱讀內容並查找標記。

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變得容易。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2018/07/04/2ed23086a322ef624431ed488263c028.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com