【问题标题】:Download local file link in html django template在 html django 模板中下载本地文件链接
【发布时间】:2017-01-08 20:25:52
【问题描述】:

基本上我有一个 django 模板,我从中显示有关文档的数据,并且我想要一个链接来下载文件。我尝试过这样的事情,但它不起作用:

<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>

“/documents/2017/filename.txt”格式的文件名位于 document.file 变量中,或者它被调用。如何在 中使用它?或者我怎样才能让它工作?

【问题讨论】:

标签: python html django download


【解决方案1】:

您应该在 Document 模型中编写一个方法,该方法应该返回文件的正确链接。

类似于:

  def get_document_url(self):
    if self.file:
      return '/documents/2017/ + self.file.name + ".txt"

然后在 html 中你可以得到它:

<a href={{document.get_document_url}}>Link</a>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-12-28
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
相关资源
最近更新 更多