I have a File
model, which store path
field - path in the filesystem to file. All files are stored in MEDIA_ROOT/files
In template I want g
In models.py
:
import os
from django.conf import settings
from django.db import models
class File(models.Model):
... (your existing File model)
@property
def relative_path(self):
return os.path.relpath(self.path, settings.MEDIA_ROOT)
(using the relpath method to strip MEDIA_ROOT
from the value of self.path
)
In your file_detail.html
(or equivalent) template:
{{ file.name }}
NB as Chris says, it's better to use a FileField
here. The above will hopefully work for your exact situation, but unless you're deeply committed to arranging things that way, I'd suggest changing to the dedicated field.