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
- Python requests - 2.8.1
- pytz - 2015.7
- API of freegeoip.net
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.