Django:将 AbstractUser class 上的 "date_joined" 字段转换为仅月和年精度

Django: converting the "date_joined" field on the AbstractUser class to only month and year precision

我有一个 Django 模型,其中 User class 继承自 AbstractUser

class User(AbstractUser):
    pass

在 Django 管理屏幕中,我看到 date_joined 字段具有月、日、年和时间精度。

我还有一个 Django 模板,代码如下:

{% block header%}
    <h2>{{ profile.username }}</h2>
    <h3>{{ profile.date_joined }}</h3>
{% endblock %}

毫不奇怪,这呈现如下:

富吧

2021 年 7 月 4 日,6:38p.m。

我想要它呈现为 只有月份和日期,如下所示:

富吧

2021 年 7 月

是否可以将 User class 上的 date_joined 字段仅转换为月份和年份?

谢谢!

是的,您可以使用 |date template filter [Django-doc]:

格式化时间戳
<h3>{{ profile.date_joined<strong>|date:"F Y"</strong> }}</h3>