gpt4 book ai didi

python - 如何在img上异步交换uri

转载 作者:太空宇宙 更新时间:2023-11-03 20:43:24 25 4
gpt4 key购买 nike

Django 存在一个问题,即:1. 模板字段中有输入和

<input class="uri-com" placeholder="URL on community" value="" onchange="addComURI(this.value)">
<img id="fg-photo-50" class="community-50" src="{% static "img/community_50.gif" %}">
  • 当您在输入中添加指向第三方资源的链接时,我想替换原始图像(位于该链接上的图像)。在这下下载到它自己不需要。我正在替换 JS 中 img 标签的值
  • document.getElementById("fg-photo-50").src = this.responseText;

    链接已被替换,但考虑到模板的连接静态,即在链接添加我的主机服务器的地址之前

    http://127.0.0.1:8000/%22https://getcim.com/RwO3inNPejmv9mTnK24R8EESIkhA/V6NXerYvQks

    (链接无效)

    我的 addComURI:

    function addComURI(comm_uri) {
    var xhr = new XMLHttpRequest();
    var params = 'comm_uri=' + encodeURIComponent(comm_uri);
    xhr.open('GET', 'test?' + params, true);
    xhr.send();

    xhr.onreadystatechange = function() {
    if (this.readyState != 4) return;
    if (this.status != 200) {
    alert( 'error: ' + (this.status ? this.statusText : 'bad request'));
    return;
    }
    document.getElementById("fg-photo-50").src = this.responseText;
    }
    }

    以及发送响应的 Django 代码:

    def request_community(request):
    comm_uri = request.GET.get("comm_uri", 1)
    fg_photo= get_community_photo(comm_uri)

    return HttpResponse(json.dumps(fg_photo), content_type='application/json')

    Who will tell you how to properly replace the link to the image?

    最佳答案

    您的 get_community_photo 函数返回一个字符串。但在将其返回到浏览器之前,您需要对其调用 json.dumps() 。这具有将其括在一对额外的引号中的效果。因此,当您将该响应文本分配给图像 src 时,会包含额外的引号:这就是 %22 的含义。

    解决方案就是不这样做。删除 json.dumps 并简单返回值:

    return HttpResponse(fg_photo)

    关于python - 如何在img上异步交换uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56741858/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com