Django admin:时区显示 [英] Django admin: timezone display

查看:102
本文介绍了Django admin:时区显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个应用程序,您可以在其中找到发生在位置的活动。

So i am making an app where you can find activities which happen at locations.

在django-admin页面上,我希望能够修改活动(

On the django-admin page i want to be able to modify Activities (which works).

但是活动有开始时间-我希望该开始时间与所在位置位于同一时区。

However an activity has a starting time - i want this starting time to be in the same timezone as the location.

因此,我希望它在活动管理页面上与该位置所在的时区显示开始时间,但保存后应将其转换为UTC时间。

So i want it to display the start time, on the activity admin page, in the same timezone as the location is in, but then when saved it should be converted to UTC time.

开始时间采用内联格式,因为它可以有多个开始时间。

The starttime is in an inline-formset, as it can have multiple start times.

我找到了一种更改日期时间的方法保存对象,但是当以嵌入式内容呈现时,我找不到修改它的方法。

I find a way to change the datetime when saving the objects, but i cant find a way to modify it when it's rendered in the inline-thing.

我如何修改在管理页面中呈现的数据?

How do i modify data as it's rendered in the admin page?

推荐答案

所以我希望它在活动管理页面上以与时区相同的时区显示开始时间该位置在,但随后在保存时应该将t转换为UTC时间。

根据Django关于时区识别形式的输入文件 https://docs.djangoproject.com /en/1.10/topics/i18n/timezones/#time-zone-aware-input-in-forms ):

According to Django's documentation on Time zone aware input in forms (https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#time-zone-aware-input-in-forms):


启用时区支持后,Django会解释当前时区中以表格形式输入的
的日期时间,并在cleaned_data中返回可感知的日期时间对象

When you enable time zone support, Django interprets datetimes entered in forms in the current time zone and returns aware datetime objects in cleaned_data.

据我了解,这就是您想要的。这将导致我们转到默认时区和当前时区 https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#default-current-time-time-zone ),其中指出:

Which from what I understood is what you want. This leads us to Default time zone and current time zone (https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#default-current-time-zone), which states:


当前时区是用于渲染的时区。

您应使用 activate()将当前时区设置为最终用户的实际时间
时区。否则,将使用默认时区。

You should set the current time zone to the end user’s actual time zone with activate(). Otherwise, the default time zone is used.

因此,请使用 activate() https://docs.djangoproject.com/en/1.10/ref/ utils /#django.utils.timezone.activate )来设置 timezone 参数,您就可以了。

So, use activate() (https://docs.djangoproject.com/en/1.10/ref/utils/#django.utils.timezone.activate) to set timezone argument and you're good to go.

这篇关于Django admin:时区显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆