Arul's Blog

Getting user timezone from IP in Django

Wed 04 November 2015 By Arulmurugan Rajaraman comments

In my recent django project, I needed to show timestamps in user's timezone. I thought it will be easier. But it took some time for me to figure out it. I'm listing the steps I followed to achieve that. This may help someone.

Packages/Services used

Step 1: Get user timezone

You can get user timezone from freegeoip API by making a get request to the API.

Step 2: Activate timezone

Then activate the timezone using the command

You can write a middleware to do this so that the timezone is activated. The middleware code can be like this.

Freegeoip provides 10,000 requests per hour by default. So if you need more number of requests, you may have to try some other API.

Now we have implemented middleware to get and activate user timezone in django. Feel free to post your comments.

comments powered by Disqus