Python Django Template cannot get name from model function
<p>While I understood that I can call a function definition from our models, I can't seem to extract the file name of my uploaded document. Below I tried the following template format but either results from my desired output:</p>

<p><strong><em>.html Version 1</em></strong></p>

<pre><code>&lt;form action="." method="GET"&gt;
{% if documents %}
   &lt;ul&gt;
      {% for document in documents %}
        &lt;li&gt; {{ document.docfile.filename }}
        &lt;input type = "submit" name="load-data" value="Add to Layer"/&gt;
        &lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
</code></pre>

<p><em>Result: It just shows the button.</em></p>

<p><strong><em>.html Version 2</em></strong></p>

<pre><code>&lt;form action="." method="GET"&gt;
{% if documents %}
   &lt;ul&gt;
      {% for document in documents %}
        &lt;li&gt; {{ document.filename }}
        &lt;input type = "submit" name="load-data" value="Add to Layer"/&gt;
        &lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
</code></pre>

<p><em>Result: It just shows the button.</em></p>

<p><strong><em>.html Version 3</em></strong></p>

<pre><code>&lt;form action="." method="GET"&gt;
{% if documents %}
   &lt;ul&gt;
      {% for document in documents %}
        &lt;li&gt; {{ document.docfile.name }}
        &lt;input type = "submit" name="load-data" value="Add to Layer"/&gt;
        &lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
</code></pre>

<p><em>Result: Prints the complete pathname (eg: /documents/2016/10/08/filename.csv) together with the button</em></p>

<p>Here's the rest of my code:</p>

<p><strong><em>models.py</em></strong></p>

<pre><code>class Document(models.Model):
     docfile = models.FileField(upload_to='document/%Y/%m/%d')

     def filename(self):
        return os.path.basename(self.file.name)
</code></pre>

<p><strong><em>views.py</em></strong></p>

<pre><code>documents = Document.objects.all()
return render(request, 'gridlock/upload-data.html',
              {
               'documents' : documents,
               'form': form
              })
</code></pre>

<p>I hope someone can explain why everything I tried:
<code>{{ document.docfile.filename }}</code> or <code>{{document.file.filename}}</code> or <code>{{document.filename}}</code> won't work either for me? Thanks!</p>

<p>I think you got pretty close with <code>{{ document.filename }}</code>, except in your models you need to change </p>

<pre><code>def filename(self):
    return os.path.basename(self.file.name)
</code></pre>

<p>into</p>

<pre><code>def filename(self):
    # try printing the filename here to see if it works
    print (os.path.basename(self.docfile.name))
    return os.path.basename(self.docfile.name)
</code></pre>

<p>in your models the field is called docfile, so you need to use self.docfile to get its value.</p>

<p>source: <a href="http://stackoverflow.com/questions/2683621/django-filefield-return-filename-only-in-template">django filefield return filename only in template</a></p>

python django file filefield