1

Using Django 1.4

I have a project that requires different times and dates to be shown and one way to simplify things is that I have included an option for users to set their timezones in their profiles. It seems that this is not good enough however so I'm wondering what would be the best way to set the users timezone automatically upon registration (assuming there is some way to fetch it from the browser or django's timezone package).

Currently I have it default to 'UTC' upon user creation but I think there is a better way of doing it.

ark
  • 749
  • 3
  • 8
  • 29
  • This may help http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript – Dillon Benson Mar 04 '15 at 04:35
  • Yea I saw that, the main problem I have is how to pass that information back to the backend upon registration – ark Mar 04 '15 at 04:40
  • 1
    You can use a hidden field and set its value in javascript – Dillon Benson Mar 04 '15 at 04:43
  • Hmm I could do it that way. Include it in the form and then pass it back down to the backend I guess – ark Mar 04 '15 at 04:45
  • @ark: see [how to select *the current (client's) timezone* in django](https://docs.djangoproject.com/en/1.4/topics/i18n/timezones/#selecting-the-current-time-zone) – jfs Mar 04 '15 at 07:56
  • related: [django 1.4 how to automatically get user's timezone from client](http://stackoverflow.com/q/10235956/4279) – jfs Mar 04 '15 at 07:57

1 Answers1

0

I recently used this:

https://github.com/Miserlou/django-easy-timezones

It uses a table of ip addresses to figure out which timezone you are in, and makes it the current timezone for every request. You don't even have to ask the user.

  • I was gonna use that it but it seems to me(I may be wrong) that every middleware you add slightly slows down the entire program by adding more bloat – ark Mar 04 '15 at 13:37
  • @ark Yeah, it adds a little overhead, but as far as I can tell, it loads up the ip table into memory, so there is no disk involved. And the middleware class itself isn't too big. The whole file is 50 lines, including imports and blank lines. Honestly, the added bloat is well worth not having to worry about timezones. Imagine if you store the tz with the user in the database, and he logs into his account while on a business trip. He's still going to be on his home tz, while in a different city/country. With this, you don't even have to worry, because his IP address will determine his current tz – Armando Alvarado Mar 04 '15 at 18:35