-1

I am building a BlogApp and I am stuck on a Problem.

What i am trying to asking :-

As everybody know that Django provides Time Zone select feature in settings.py like

TIME_ZONE = 'Asia/Sanghai'

AND i have made a instance

date = models.DateTimeField(auto_now_add=True)

AND

date_added = models.DateTimeField(null=True,default=timezone.now) in models.py

So my time is of Asia AND whenever i deploy my webapp and A user from Europe try to access the webapp and try make a BlogPost, So what will be the time for Europe's User ?

  • My selected time ( Asia/Sanghai ) ?

  • OR `Europe's User Time ?

If his/her time will be My selected time then how can i make it like : When someone visit the site then show time according to User's timezone ?

I have no idea, What will be the timezone when someone try to access the site from different Country.

Any help would be Appreciated. Thank You in Advance.

Lars
  • 1,234
  • 1
  • 8
  • 31
  • 1
    Does this answer your question? [Determine a user's timezone](https://stackoverflow.com/questions/13/determine-a-users-timezone) – Austin Greco Apr 17 '21 at 04:34
  • 1
    NO. My question is different and I am talking about the `default setting`, UNDERSTOOD ? – Lars Apr 17 '21 at 04:36
  • 1
    A server has no default way to determine a client's timezone. It will be the timezone of the server itself, for all clients. If you want to show the local time for a user, you will likely need to use javascript to get the timezone offset, or ask the user to select their timezone and store it on the server. The link above has more details. – Austin Greco Apr 17 '21 at 04:40
  • So if a user from America then Will tim automatically be set ? – Lars Apr 17 '21 at 04:42
  • If the server is set to `Asia/Shanghai` then it will be `Asia/Shanghai` for every user, because the server can not determine timezone by default. You would have to ask the user to select a timezone and store it, or use javascript to get the timezone offset and change it in the client's browser. – Austin Greco Apr 17 '21 at 04:45
  • 1
    Simple solution would be to provide a list of time zones with associated country to the user when he/she is registering to your site. Once the user select the country you can store it in their profile and accordingly calculate the timezone by giving that selected timezone to pytz module. – Ahtisham Apr 17 '21 at 07:44
  • You're absolutely Right, BUT I've made a country select field BUT how can attach it with timezone ? **Suppose**, A user selects `Egypt` then how can i select time for it ? – Lars Apr 17 '21 at 07:45

1 Answers1

1

You can return a list of timezones from the backend to the user while they are registering to your site and store the selected timezone in their profile something like this:

from django.shortcuts import render
import pytz

class RegisterView(View):

    def get(self, request):
        # view code
        return render(request, 'signup.html', {
            'country_list': pytz.all_timezones
        })

    def post(self, request):
        timezone = request.POST.get('selected_timezone')
        profile = Profile(timezone=timezone, ...)
        profile.save()

and you can get a specific time given a timezone like this:

from datetime import datetime
import pytz

tz = pytz.timezone('Europe/Berlin')
berlin_now = datetime.now(tz)
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
  • Thank you very Much for your Help. BUT where is the the `Profile Model's country choicefield` ? – Lars Apr 17 '21 at 08:52