pyknight202
pyknight202

Reputation: 1487

Django's displayed datetime doesn't change based on the user's local timezone

models.py

from django.db import models

# Create your models here.

class Topic(models.Model):
    """A topic the user is learning about."""
    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        """Returns a string representation of the model."""
        return self.text

class Entry(models.Model):
    """Something specific learned about a topic."""
    topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = 'entries'

    def __str__(self):
        """Return a string representation of the model."""
        if len(self.text) > 50:
            return f"{self.text[:50]}..."
        else:
            return self.text

template value {{ entry.date_added|date:'M d, Y g:i' }}

In a template, if I display the date_added variable, it seems that the time is 8 hours behind from my computer's time. I tried to change the timezone in settings, but I was met with ValueError: Incorrect timezone setting.

Edit: USE_TZ is set to true.

In the following template code, localtime on doesn't affect the date, strangely only displaying the server time.

            {% load tz %}

            {% timezone "Europe/Paris" %}
                <p>Paris time: {{ entry.date_added|date:'M d, Y g:i' }}</p>
            {% endtimezone %}

            {% timezone None %}
                <p>Server time: {{ entry.date_added|date:'M d, Y g:i' }}</p>
            {% endtimezone %}

            {% localtime on %}
                <p>Local Time: {{ entry.date_added|localtime }}</p>
            {% endlocaltime %}

Upvotes: 3

Views: 1405

Answers (2)

pyknight202
pyknight202

Reputation: 1487

https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Setting the TIME_ZONE variable in settings to the string associated with a 3 letter time zone code in the table above worked.

e.g TIME_ZONE = 'Country/City_Name'


EDIT: Setting the server time to UTC and then providing the user their local time converted using Javascript would work, and is probably the best method of going about this.

Upvotes: 0

Hutch
Hutch

Reputation: 854

In you settings.py file your default time zone is set to

TIME_ZONE = 'UTC'

You're going to want to leave this as-is so you don't encounter issues with DST. Once you obtain your object from the database, it will be up to you to convert to the local timezone. In order to get the local timezone, you can ask the user directly and save the timezone to their profile using something like pytz.

See the example from the django docs on timezones.

If you are not concerned about showing the proper timezone for all users and just want to quickly change it for your own sake, you can set the timezone in your template.

{% load tz %}

{% timezone "Europe/Paris" %}
    Paris time: {{ value }}
{% endtimezone %}

{% timezone None %}
    Server time: {{ value }}
{% endtimezone %}

Upvotes: 1

Related Questions