django filefield仅在模板中返回文件名 [英] django filefield return filename only in template
本文介绍了django filefield仅在模板中返回文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
File.name:文件的名称,包括MEDIA_ROOT的相对路径。
我想要的是像.filename这样只会给我文件名,而不是路径
:
{%下载下载%}
< div class =download>
< div class =title> {{download.file.filename}}< / div>
< / div>
{%endfor%}
这会给我一些像myfile.jpg
谢谢
解决方案
在您的模型定义中:
import os
类文件(models.Model):
file = models.FileField()
。
def filename(self):
return os.path.basename(self.file.name)
I've got a field in my model of type FileField. This gives me an object of type type File, which has the following method:
File.name: The name of the file including the relative path from MEDIA_ROOT.
What I want is something like .filename that will only give me the filename and not the path as well
something like:
{% for download in downloads %}
<div class="download">
<div class="title">{{download.file.filename}}</div>
</div>
{% endfor %}
which would give something like myfile.jpg
thanks
解决方案
In your model definition:
import os
class File(models.Model):
file = models.FileField()
...
def filename(self):
return os.path.basename(self.file.name)
这篇关于django filefield仅在模板中返回文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文