In my model, I have a defined a FileField
which my template displays it as a link. My problem is that the linked file displays the url as the name. What shows on html page is:
Uploaded File: ./picture.jpg
I've looked on the DjangoDocs regarding file names and a previous S.O. question, but just can't figure it out.
How can I:
- Have it display a different name, not a url.
- Allow the admin who uploaded the file to give it a name, which would then be viewed on the template.
my models.py:
class model_name(models.Model):
attachment = models.FileField()
my views.py (if entry exists, display it, if not, return message):
from django.core.files import File
from vendor_db.models import model_name
def webpage(request, id):
try:
variable = model_name.objects.get(id=id)
except model_name.DoesNotExist:
raise Http404('This item does not exist')
return render(request, 'page.html', {
'variable': variable,
})
my page.html:
<p>Uploaded File: <a href="{{ variable.attachment.url }}">{{ variable.attachment }}</a></p>