I am creating a form in Django. I have to put a input type field, which only stores the timezone in database(which will be chosen by a user from a drop Down list at form). I am not able to find out any approach to create this timezone model and how it will return the local time according to saved timezone. I choose following field but it stores also minute hours and second also
timestamp = models.DateTimeField()
Drop down list should be in form of : ... GMT +5:30 GMT +6:00...and so on.
Neither django nor python provide a set of timezones for you to use. For that, you will need an additional module like pytz . You can get a list of all timezones like this: >>> import pytz >>> pytz.
First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.
When USE_TZ is False, this is the time zone in which Django will store all datetimes. When USE_TZ is True, this is the default time zone that Django will use to display datetimes in templates and to interpret datetimes entered in forms.
I think the above answers are all correct, but I leave mine here as an simple example...
class UserProfile(models.Model): import pytz TIMEZONES = tuple(zip(pytz.all_timezones, pytz.all_timezones)) # ... timezone = models.CharField(max_length=32, choices=TIMEZONES, default='UTC') # ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With