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:
.html Version 1
<form action="." method="GET">
{% if documents %}
<ul>
{% for document in documents %}
<li> {{ document.docfile.filename }}
<input type = "submit" name="load-data" value="Add to Layer"/>
</li>
{% endfor %}
</ul>
Result: It just shows the button.
.html Version 2
<form action="." method="GET">
{% if documents %}
<ul>
{% for document in documents %}
<li> {{ document.filename }}
<input type = "submit" name="load-data" value="Add to Layer"/>
</li>
{% endfor %}
</ul>
Result: It just shows the button.
.html Version 3
<form action="." method="GET">
{% if documents %}
<ul>
{% for document in documents %}
<li> {{ document.docfile.name }}
<input type = "submit" name="load-data" value="Add to Layer"/>
</li>
{% endfor %}
</ul>
Result: Prints the complete pathname (eg: /documents/2016/10/08/filename.csv) together with the button
Here's the rest of my code:
models.py
class Document(models.Model):
docfile = models.FileField(upload_to='document/%Y/%m/%d')
def filename(self):
return os.path.basename(self.file.name)
views.py
documents = Document.objects.all()
return render(request, 'gridlock/upload-data.html',
{
'documents' : documents,
'form': form
})
I hope someone can explain why everything I tried:
{{ document.docfile.filename }}
or {{document.file.filename}}
or {{document.filename}}
won't work either for me? Thanks!