如何从文件 views.py 的函数中 return 图像地址到 Django 模板?
How can I return image address to Django template from a function of the file views.py?
我需要使用 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”。
也许试试:
src="{{object.image.url}}"
用image.url你得到图片的路径。
我们只需要在 return 行进行更改,它应该如下所示:-
return HttpResponseRedirect(filepath)
使用后效果很好!!
我需要使用 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”。
也许试试: src="{{object.image.url}}"
用image.url你得到图片的路径。
我们只需要在 return 行进行更改,它应该如下所示:-
return HttpResponseRedirect(filepath)
使用后效果很好!!