227👍
✅
In your model definition:
import os
class File(models.Model):
file = models.FileField()
...
def filename(self):
return os.path.basename(self.file.name)
60👍
You can do this by creating a template filter:
In myapp/templatetags/filename.py
:
import os
from django import template
register = template.Library()
@register.filter
def filename(value):
return os.path.basename(value.file.name)
And then in your template:
{% load filename %}
{# ... #}
{% for download in downloads %}
<div class="download">
<div class="title">{{download.file|filename}}</div>
</div>
{% endfor %}
👤rz.
- [Django]-Get the list of checkbox post in django views
- [Django]-Django form: what is the best way to modify posted data before validating?
- [Django]-How to check if a user is logged in (how to properly use user.is_authenticated)?
3👍
You could also use the cut
filter in your template
{% for download in downloads %}
<div class="download">
<div class="title">{{download.file.filename|cut:'remove/trailing/dirs/'}}</div>
</div>
{% endfor %}
- [Django]-Getting the SQL from a Django QuerySet
- [Django]-How do I filter query objects by date range in Django?
- [Django]-Proper way to handle multiple forms on one page in Django
-3👍
You can access the filename from the file field object with the name property.
class CsvJob(Models.model):
file = models.FileField()
then you can get the particular objects filename using.
obj = CsvJob.objects.get()
obj.file.name property
- [Django]-Django, Turbo Gears, Web2Py, which is better for what?
- [Django]-How can I avoid "Using selector: EpollSelector" log message in Django?
- [Django]-Django MEDIA_URL and MEDIA_ROOT
Source:stackexchange.com