[英]Download local file link in html django template
Basically I have a django template from which I show data about a document and I want a link to download the file.基本上我有一个 django 模板,我可以从中显示有关文档的数据,并且我想要一个链接来下载文件。 I tried something like this but it is not working:
我尝试过这样的事情,但它不起作用:
<div class="metadata-container">
<div class="metadata-title">
<div>Version</div>
<div>Author</div>
<div>File</div>
</div>
<div class="metadata-content">
<div>{{ document.version }}</div>
<div>{{ document.author }}</div>
<a href=document.file download><div>{{ document.file }}</div>
</div>
</div>
The filename in format '/documents/2017/filename.txt' is in document.file variable or however it's called.格式为 '/documents/2017/filename.txt' 的文件名在 document.file 变量中,或者无论如何它被调用。 How can I use it in the ?
我怎样才能在 ? or how can I make it work?
或者我怎样才能让它工作?
You should write a method inside your Document model a method which should return the correct link to the file.您应该在 Document 模型中编写一个方法,该方法应该返回文件的正确链接。
Similar to:相似:
def get_document_url(self):
if self.file:
return '/documents/2017/ + self.file.name + ".txt"
And then inside html you can get it with:然后在 html 中你可以得到它:
<a href={{document.get_document_url}}>Link</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.