Ouput timezone knows django datetime fields without filters

Hi i have upgraded to django 1.4 and i want to take advantage of timezone support, i got some datetime fields stored in postgres and they were saved assuming my city timezone as soon as i installed

USE_TZ = True

      

And set the time zone for my city. Date filter tags in my templates output the correct hour (time).

{{ concert.datetime|date:'f' }}

      

The problem is this: I am using datetime to generate my urls like:

{% url event artist_slug=concert.slug_name hour=concert.datetime.hour %}

      

And they are not synchronized correctly, the hour is still in UTC and that is changing my links, something I cannot do, it will lose the entire page rank and link to many site links is not possible, not to mention that it looks strange. that the url has a different hour than the one advertised. I've tried this:

{% url event artist_slug=concert.slug_name hour=concert.datetime.hour|date:'H' %}

      

Without success, the date filter tag is not applied and an exception is thrown. I have a fairly large codebase and lots of templates, is there a way to fix this without using an accesor that returns a datetime timezoned?

Thank.

+1


source to share


2 answers


In fact, the Django documentation states:

Even if your website is only available in one time zone, its a good practice to store data in UTC in your database. Daylight Saving Time (DST) is one of the main reasons. Many countries have a DST system where clocks move forward in spring and back in autumn. If you are working in local time, you will likely run into errors twice a year when transitions occur. (The pytz documentation discusses these issues in more detail.) This probably doesn't matter to your blog, but it's a problem if you overestimate or underestimate your customers by one hour, twice a year, every year. The solution to this problem is to use UTC code in your code and use local time only when interacting with end users.

Further

When timezone support is enabled, Django uses timezone-specific datetime objects. If your code creates datetime objects, they should know too. In this mode, the example above:



import datetime
from django.utils.timezone import utc

now = datetime.datetime.utcnow().replace(tzinfo=utc)

      

Reporting Time Zones in Templates When you turn on time zone support, Django converts known datetime objects to the current time zone when they're displayed in templates. This is very similar to the localization format.

And finally, nothing without decapitation: https://docs.djangoproject.com/en/1.6/topics/i18n/timezones/#template-tags

+1


source


Unfortunately, the only way I have worked with is to convert the date to the users' timezone and provide a custom template tag to get the snippet I want, e.g .:

{% url event artist_slug=concert.slug_name hour=concert.datetime|localtime|hour_of_day %}

      



Where hour_of_day is a custom tag that returns the current hour at the correct local time.

0


source







All Articles