繁体   English   中英

Django,如何更新图像

[英]Django, How to update image

我希望标题足以理解我的问题,如下图所示,我可以从我的数据库中获取图片,问题是当我更新数据但没有更改显示的图片时,它会出错,错误的原因是我没有从 html 获取任何数据

<label for="myfile3">
    <img id="output3"  src="{{selectbanner.image.url}}" style="height:225px;width:250px;" class="subimage"/><br>
    <input type="file" accept="myfile" id="myfile3" name="image" value="{{selectbanner.image.url}}" onchange="loadFile3(event)"  >
</label>
<script>
     var loadFile3 = function(event) {
     var reader = new FileReader();
     reader.onload = function(){
     var output3 = document.getElementById('output3');
     output3.src = reader.result;
     };
     reader.readAsDataURL(event.target.files[0]);
     };
</script>

这是我的views.py

image = request.FILES.get('image')
print(image)

update = Banner.objects.get(id=banner_id)
    update.image = image
    update.title = Title
    update.sub_title = Sub
    update.description = Description

    update.save()

    return redirect('Banners')

在此处输入图片说明

首先,我想说您应该参考之前提出的相关问题。 参考这些:

这个

这个

和这个

暂无
暂无

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

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