简体   繁体   English

Django admin:时区显示

[英]Django admin: timezone display

So i am making an app where you can find activities which happen at locations. 所以我正在创建一个应用程序,您可以在其中找到在各个位置发生的活动

On the django-admin page i want to be able to modify Activities (which works). 在django-admin页面上,我希望能够修改活动(有效)。

However an activity has a starting time - i want this starting time to be in the same timezone as the location. 但是活动有一个开始时间 - 我希望这个开始时间与位置在同一时区。

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. 所以我希望它在活动管理页面上显示与位置相同的时区中的开始时间,但是当保存时它应该转换为UTC时间。

The starttime is in an inline-formset, as it can have multiple start times. starttime采用内联formset,因为它可以有多个开始时间。

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? 如何修改管理页面中呈现的数据?

" 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. " 所以我希望它在活动管理页面上显示与位置相同的时区中的开始时间,但是当保存时,它应该转换为UTC时间。

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关于表格中时区感知输入的文档( https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#time-zone-aware-input-in-forms ):

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. 当您启用时区支持时,Django会解释在当前时区中的表单中输入的日期时间,并在cleaning_data中返回有意识的日期时间对象。

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: 这导致我们进入默认时区和当前时区https://docs.djangoproject.com/en/1.10/topics/i18n/timezones/#default-current-time-zone ),其中指出:

The current time zone is the time zone that's used for rendering. 当前时区是用于渲染的时区。

You should set the current time zone to the end user's actual time zone with activate() . 您应该使用activate()将当前时区设置为最终用户的实际时区。 Otherwise, the default time zone is used. 否则,使用默认时区。

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. 因此,使用activate()https://docs.djangoproject.com/en/1.10/ref/utils/#django.utils.timezone.activate )来设置时区参数,你就可以了。

Try to set 尝试设置
USE_L10N = False on settings.py: USE_L10N = False上的USE_L10N = False

https://docs.djangoproject.com/en/1.9/ref/settings/#use-l10n https://docs.djangoproject.com/en/1.9/ref/settings/#use-l10n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM