I have the following django model:
RESOURCE_DIR = os.path.join(settings.MEDIA_ROOT, 'resources')
class Resource(models.Model):
title = models.CharField(max_length=255)
file_name = models.FilePathField(path=RESOURCE_DIR, recursive=True)
and I want to give the URL to the file in a template so that the user can view it or download it.
If I use {{ resource.file_name }} in the template it outputs the full path of the file on the server, e.g. if RESOURCE_DIR='/home/foo/site_media/media' it outputs '/home/foo/site_media/media/pdf/file1.pdf' whereas what I want is 'pdf/file1.pdf'. In the admin or in a modelform the option is displayed as '/pdf/file1.pdf' in the select widget. So obviously it is possible to do what I asking. Of course the extra slash is not important. If I were setting recursive=False then I could just remove the part of the path before the last slash.
How can I get the same result as the modelform or admin?