![](/img/trans.png)
[英]How make a call to a function in views.py file in Django from ajax-html in template folder?
[英]How can I return image address to Django template from a function of the file views.py?
我需要使用Django 模板顯示圖像,但不知道如何從文件views.py的 function 返回“圖像地址”
圖片地址將根據項目的“ id ”存儲(一個項目有一個與之關聯的圖片)。
這是我正在編寫的views.py文件的function:-
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 HttpResponseRedirect(filepath)
使用后效果很好!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.