0

I am building a BlogApp AND i am stuck on a Problem.

What i am trying to do :-

I am trying to store timezone in every field ( post date, comment date ) according to User's Country.

Suppose, A user is registered account on webapp from Brazil then i am trying to automatically set time zone of Brazil for the User.

But i have tried many answers LIKE THIS and LIKE THIS but nothing worked for me. Some told that it should be done through JavaScript BUT i have no idea idea how can i do it.

When i try to register then it automatically sets the timezone which i have settled in settings.py.

TIME_ZONE = 'UTC'

What have i tried :-

  • I have also tried localize ( according to docs ) BUT still showing the same time.

models.py

class Post(models.Model):
    post_owner = models.ForeignKey(User,default='',null=True,on_delete = models.CASCADE)
    date_added = models.DateTimeField(null=True,default=timezone.now)

settings.py

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

new_post.html

{% load l10n %}

{% localize on %}
    {{ form.date_added|as_crispy_field }}
{% endlocalize %}

all_posts.html

{% load l10n %}

{% localize on %}
    {{ post.date_added|as_crispy_field }}
{% endlocalize %}

I have set UTC in settings BUT i am in Asia/Kolkata timezone. So it should localize the timezone into my Local Time. BUT it is still in UTC.

Any help would be appreciated.

Thankyou in advance.

  • See [Format localization (Django docs)](https://docs.djangoproject.com/en/3.2/topics/i18n/formatting/) – Abdul Aziz Barkat Apr 27 '21 at 05:18
  • @AbdulAzizBarkat, Will it also change `timezone` according to user's country ? –  Apr 27 '21 at 05:20
  • Why change the settings for each user? Django will automatically try to localize any date / time formats, timezones etc. according to a users locale as long as `USE_L10N` is set to `True`. – Abdul Aziz Barkat Apr 27 '21 at 05:24
  • @AbdulAzizBarkat, How django will know which country i am in AND which time zone to convert ? –  Apr 27 '21 at 05:33
  • @AbdulAzizBarkat, I have edited my question, Please have a look. –  Apr 27 '21 at 05:47

0 Answers0