Python 如何在img上异步交换uri
Django存在一个问题,即: 1.模板中有字段条目和Python 如何在img上异步交换uri,python,django,Python,Django,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" %}"> 该链接将被替换,但要考虑到已连接到模板的静态信息,
<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" %}">
该链接将被替换,但要考虑到已连接到模板的静态信息,即在该链接添加我的主机服务器地址之前
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)
get_community_photo函数返回一个字符串。但是,在将其返回到浏览器之前,需要对其调用
json.dumps()
。这样做的效果是将其包装在一对额外的引号中。因此,当您将响应文本分配给图像src时,会包含额外的引号:这就是%22
的含义
解决办法就是不这样做。删除json.dumps
,只需返回值:
return HttpResponse(fg_photo)
我们需要查看整个
addComURI
函数,以及发送响应的Django代码。谢谢您现在您需要显示get\u community\u photo
。关键是,该响应中包含什么?请求\社区返回一个url为“”的字符串。如果我硬编码将这个字符串设置为我的js函数addComURI:document.getElementById(“fg-photo-50”).src='10〕https://pp.userapi.com/RwO3inNPejmv9mTnK24R8EOguX3ZTWvQESIkhA/V6NXerYvQks.jpg';图像更新。我们需要查看整个addComURI
函数,以及发送响应的Django代码。好的,我正在更新我的问题。谢谢您现在您需要显示get\u community\u photo
。关键是,该响应中包含什么?请求\社区返回一个url为“”的字符串。如果我硬编码将这个字符串设置为我的js函数addComURI:document.getElementById(“fg-photo-50”).src='10〕https://pp.userapi.com/RwO3inNPejmv9mTnK24R8EOguX3ZTWvQESIkhA/V6NXerYvQks.jpg';代码>html页面上的图像更新。