2

Basically I have a django template from which I show data about a document and I want a link to download the file. 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. How can I use it in the ? or how can I make it work?

Mocktheduck
  • 1,333
  • 1
  • 22
  • 43
  • 1
    Possible duplicate of [Django download a file](http://stackoverflow.com/questions/36392510/django-download-a-file) – Sayse Jan 08 '17 at 20:38

1 Answers1

3

You should write a method inside your Document model a method which should return the correct link to the file.

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:

<a href={{document.get_document_url}}>Link</a>
Alexander Tyapkov
  • 4,837
  • 5
  • 39
  • 65