【发布时间】:2022-01-23 14:07:21
【问题描述】:
我需要使用 Django 模板显示图像,但不知道如何从文件 views.py 的函数中返回“图像地址”
图片地址会根据项目的“id”存储(一个项目有与之关联的图片)。
这是我正在编写的 views.py 文件的功能:-
def file_path(request, awp_id):
Component_Progress_Update_inst = get_object_or_404( Component_Progress_Update, pk=awp_id)
filepath = Component_Progress_Update_inst.Supporting_documents.name
filepath = "/media/"+filepath
print(filepath)
# Returning the file path
return HttpResponse(filepath)
下面是图片标签,我写的是HTML文件:-
<img src="{% url 'component_progress:file_path' awp.id %}" alt="Supporting image" width="200" height="200">
但是图像没有显示出来,而不是图像这个“替代文本”被打印出来了。
虽然我尝试直接显示图像 -> 通过直接在图像“src”中写入该地址。
【问题讨论】:
标签: python django django-views django-templates