0π
β
I figured out that you can retrieve the path argument to the FilePathField using resource._meta.get_field(βfile_nameβ).path It seems best to do this in the model. So the model becomes:
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)
def url(self):
path = self._meta.get_field('file_name').path
return self.file_name.replace(path, '', 1)
then in the template you can put:
{{ MEDIA_URL }}resources{{ resource.url }}
π€gaumann
1π
the below leaves in the leading path separator, which may not be the forward slash a url needs
def url(self):
path = self._meta.get_field('file_name').path
return self.file_name.replace(path, '', 1)
so slight improvement
def url(self):
path = self._meta.get_field('icon').path
return "/" + self.icon[len(path)+1:]
π€user321393
- [Django]-Best practice β Django multisite
- [Django]-Override Django User Manager to only return active users in queries
0π
this is kind of cheating:
{{ resource.file_name|cut:resource.file_name.path }}
not tested.
π€Brandon Henry
- [Django]-Using Django, I want to find all of the published events after today, but only from the nearest month containing events
- [Django]-Django: How to avoid duplicated html id for showing field twice in the same form?
Source:stackexchange.com