How can I give the user switch:
- date time format
- timezone
both to display different dateTime
as the user determined.
I want to enable this options in admin Django or UI.
Not manually settings page.
How can I give the user switch:
both to display different dateTime
as the user determined.
I want to enable this options in admin Django or UI.
Not manually settings page.
First of all, you have to create model (UserFormats) for storing this data from users. Two fields - datetime_format
and timezone
. These fields will be ChoiceField - example of implementation you can found here https://stackoverflow.com/a/24404791/5450939 . Keys are numbers and values are formats what you want (you can import from pytz
import pytz
pytz.all_timezones
). This model will be ForeignKey to your User model.
Also, add Boolean field like a is_user_datetime_format_enabled
and a is_user_timezone_format_enabled
.
It will allow you to switch options in the Django admin for every user.
Then you can check in your code or template:
import datetime
today = datetime.date.today()
if user.userformats.is_user_datetime_format_enabled:
today.strftime(user.userformats.datetime_format)
else:
today.strftime("%d/%m/%y"). # some default formatting
...
With timezone the same situation.