简体   繁体   中英

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).

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.

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?

" 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. "

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 ):

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.

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:

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() . 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.

Try to set
USE_L10N = False on settings.py:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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