0

How can I give the user switch:

  1. date time format
  2. 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.

Lemayzeur
  • 8,297
  • 3
  • 23
  • 50
Adi Ep
  • 509
  • 1
  • 5
  • 22

1 Answers1

0

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.

Sergey Miletskiy
  • 477
  • 2
  • 5
  • 13