在 django admin 中检测时区

发布于 2025-01-11 20:29:31 字数 512 浏览 2 评论 0 原文

我正在开发一个带有 Django 后端的项目(只有后端和默认管理门户,没有网站),其中管理门户由欧洲和美国的人们使用。 因此,管理门户中的日期时间以使用它的人的本地时区显示非常重要。
例如,在某些模型中,我显示实例的创建日期。我需要将这些日期显示在访问管理门户的人员的时区中。

我已经搜索了实现此目的的解决方案(例如 文档,还有这个包),但我发现的所有解决方案似乎都是为了检测访问自定义网站的最终用户的时区,而不是默认的管理门户。

我正在使用 Django 2.2 和 Python 3.8。

I'm working in a project with a Django backend (only backend and a default admin portal, no site) where the admin portal is used by people both in Europe and US.
Because of this, it's important that the datetimes in the admin portal are displayed in the local timezone of whomever is using it.
For example, in some models I display the creation date of instances. I need those dates to be displayed in the timezone of whomever accesses the admin portal.

I've searched for solutions to achieve this (such as suggested in the docs, but also this package) but all the solutions I've found seem to be made for detecting the timezone of end-users accessing a custom website, not the default admin portal.

I'm using Django 2.2 and Python 3.8.

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

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

发布评论

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

评论(1

貪欢 2025-01-18 20:29:31

实现此目的的方法之一是使用 Django ModelAdmin 中的自定义字段。

参考:

custom_admin_field

get_local_time< /a>


from django.contrib import admin
from django.db import models

class AnItem(models.Model):
    title = models.CharField(max_length=150)
    creation_date = models.DateField()

    @admin.display(description='Local Time')
    def local_time_of_creation(self):
        local_time = write_logic_to_get_the_local_time_here 
        return local_time

class AnItem(admin.ModelAdmin):
    list_display = ('name', 'local_time_of_creation')

One of the methods to achieve this is by using a custom field in Django ModelAdmin.

References for:

custom_admin_field

get_local_time

from django.contrib import admin
from django.db import models

class AnItem(models.Model):
    title = models.CharField(max_length=150)
    creation_date = models.DateField()

    @admin.display(description='Local Time')
    def local_time_of_creation(self):
        local_time = write_logic_to_get_the_local_time_here 
        return local_time

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