Django FileField:如何仅返回文件名(在模板中)

发布于 2024-08-29 21:06:10 字数 714 浏览 2 评论 0原文

我的模型中有一个类型为 FileField< /代码>。这给了我一个 File,它有以下方法:

File.name:文件的名称,包括相对路径 MEDIA_ROOT

我想要的是类似“.filename”的东西,它只会给我文件名,而不是路径,例如:

{% for download in downloads %}
  <div class="download">
    <div class="title">{{download.file.filename}}</div>
  </div>
{% endfor %}

这会给出类似 myfile.jpg 的东西

I've got a field in my model of type FileField. This gives me an object of 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风和你 2024-09-05 21:06:10

在您的模型定义中:

import os

class File(models.Model):
    file = models.FileField()
    ...

    def filename(self):
        return os.path.basename(self.file.name)

In your model definition:

import os

class File(models.Model):
    file = models.FileField()
    ...

    def filename(self):
        return os.path.basename(self.file.name)
琴流音 2024-09-05 21:06:10

您可以通过创建模板过滤器来做到这一点:

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)

然后在您的模板中:

{% load filename %}

{# ... #}

{% for download in downloads %}
  <div class="download">
      <div class="title">{{download.file|filename}}</div>
  </div>
{% endfor %}

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 %}
长亭外,古道边 2024-09-05 21:06:10

您还可以使用 cut 过滤器< /a> 在你的模板中

{% for download in downloads %}
  <div class="download">
    <div class="title">{{download.file.filename|cut:'remove/trailing/dirs/'}}</div>
  </div>
{% endfor %}

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 %}
浅浅淡淡 2024-09-05 21:06:10

使用 Pathlib 的现代解决方案:

from pathlib import Path

class File(models.Model):
    file = models.FileField()
    ...

    def filename(self):
        return Path(self.file.name).name

A modern solution using Pathlib:

from pathlib import Path

class File(models.Model):
    file = models.FileField()
    ...

    def filename(self):
        return Path(self.file.name).name
樱花细雨 2024-09-05 21:06:10

您可以使用 name 属性从文件字段对象访问文件名。

class CsvJob(Models.model):

    file = models.FileField()

然后您可以使用获取特定对象文件名。

obj = CsvJob.objects.get()
obj.file.name property

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文