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 specify the URL of the file in the template so that the user can view or download it.
If I use {{ resource.file_name }} in the template, it displays the full path to the file on the server, for example. if RESOURCE_DIR='/home/foo/site_media/media' outputs '/home/foo/site_media/media/pdf/file1.pdf' , whereas I want 'pdf/file1.pdf' . In admin or modelform, the option is displayed as '/pdf/file1.pdf' in the selection element. Therefore, obviously, you can do what I ask. Of course, an additional slash is not important. If I set recursive=False , then I could just delete part of the path to the last slash.
How can I get the same result as modelform or admin?
source share