如何在Django Admin界面中将日期的日期从届时更改为日期?

发布于 2025-01-31 04:51:07 字数 1193 浏览 1 评论 0原文

我是Django的新手,因此任何帮助都将不胜感激, 我有一个包含3个字段的模型,其中之一是日期,我想更改Django Admin的日期显示(现在是 2022-05-20 ,但我希望它是 20-05-2022 )。

我该怎么做!

这是我的 models.py:

class reports(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    project_name = models.CharField(verbose_name="Project name", max_length=60)
    date = models.DateField(verbose_name="Date", default=date.today)
    def __str__(self):
        return str(self.user)

这是我的

class reportsAdmin(admin.ModelAdmin):
    @admin.display(description='date')
    def admin_date(self, obj):
        return obj.date.strftime('%d-%m-%Y')

    list_display = ('user', 'project_name', 'admin_date')
admin.site.register(reports, reportsAdmin)

管理

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = False
USE_TZ = True

, 我的格式 2022-05-20 (年以上)。

我如何正确地更改格式日期为 django admin中的日期 ???

在此处输入图像描述

Am new in Django so any help will be appreciated,
I have a model that contain 3 fields , one of them is date , i want to change the display of date in Django Admin(now it's 2022-05-20 but i want it to be 20-05-2022).

How i can do that please !

this is my models.py:

class reports(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    project_name = models.CharField(verbose_name="Project name", max_length=60)
    date = models.DateField(verbose_name="Date", default=date.today)
    def __str__(self):
        return str(self.user)

and this is my admin.py:

class reportsAdmin(admin.ModelAdmin):
    @admin.display(description='date')
    def admin_date(self, obj):
        return obj.date.strftime('%d-%m-%Y')

    list_display = ('user', 'project_name', 'admin_date')
admin.site.register(reports, reportsAdmin)

and in my settings.py:

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = False
USE_TZ = True

but nothing work , still display to me in format 2022-05-20(year-month-date).

How i can correctly change the format date to be date-month-year in Django admin???

enter image description here

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

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

发布评论

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

评论(1

舞袖。长 2025-02-07 04:51:07

您的代码确实将日期转换为所需的格式,在显示所有报告的列表页面中。我认为您想要的东西无法通过ReportsAdmin类来完成。您需要的是更改可以通过更改管理模板来完成的输入元素。 ,但我不确定是否可以做到这一点,因为这是输入的日期。

edit:我发现这个问题是,第三和第二个答案提到了一些您可以在html中实现这一目标的方法
是否有任何方法可以更改输入类型=“ date”格式?

Your code does convert the date to the format you want in the list page where all the reports are displayed. I don't think that what you want can be accomplished with the reportsAdmin class. What you need is to change the input element that can maybe be done by changing the admin templates. But I am not sure that it can be done at all because that is how the input works for dates.

Edit: I found this question that 3rd and 2nd answers mention some ways you can accomplish this in HTML
Is there any way to change input type="date" format?

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