Monday, August 31, 2015

Re: TIME ZONE DJANGO SAVE DATETIMEFIELD

> Hello is the first time I write on the list, I have little time learning
> Django and presented me a drawback. I have a model called Bill, which has a
> field called date of sale, and I need to store the date and time of the sale
> but the client time, and now it is keeping the server time. As I keep the
> date and time correctly in the model?
> I have some variables in established settings:
>
> USE_TZ = True
> TIME_ZONE = 'America / Caracas'
>
> /********************models.py ***********/
>
> class Factura(models.Model):
> fecha = models.DateTimeField()

Have you validated that the time being stored is incorrect? I'm
willing to bet that the date and time in the database is actually
being stored in UTC, but the output in the templates is being
displayed with the undesired offset. Check out this page on handling
output of DateTime fields:

https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#time-zone-aware-output-in-templates

https://docs.djangoproject.com/en/1.8/topics/i18n/timezones/#default-time-zone-and-current-time-zone

Actually that whole page is a good read. Basically you need to ensure
that your DateTime is being saved in UTC (or at the very least,
everything being stored has an offset attached to it so that the
correct date/time/timezone can be calculated). Django is probably
already doing that, but you'll need to set the current time zone for a
particular user so that the templates output the right DateTime.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWap7hPzDzD-5bVK0f9vvOVvBXQ5xNDkqBCPYGdRNu1-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment