0

I have the Django project with the Model to save date_added information. Here's the Model code.

class Outputting(models.Model):
    date_added = models.DateTimeField(auto_now_add=True)

And I want to retrieve such date_added information and demonstrate it as the local time in the html. How could I make the transformation? Here's the view.py and html template, but it still show the UTC time.

view.py

def results(request):
    user = request.user
    data = Outputting.objects.filter(owner=user).order_by('id').values_list('id','date_added')
    return render(request, "xx.html", {"data": data})

Html:

<tr>
{% for i in data %}
<td style="word-wrap:break-word;"><div class="panel-body" type="datetime-local"><small>{{ i.1|date:'M d Y H:i:s' }}</small></div></td>
</tr class="something">
{% endfor %}
halfer
  • 19,824
  • 17
  • 99
  • 186
Thomas.Q
  • 377
  • 1
  • 4
  • 12

3 Answers3

0

In your settings.py change the time zone location

TIME_ZONE = 'Africa/Abidjan'

if you don't know your location you can list all available time_zone options by running below command in the Django shell:

In [40]: import pytz
In [41]: pytz.all_timezones
Out[42]: 
['Africa/Abidjan',
 'Africa/Accra',
 'Africa/Addis_Ababa',
 ...]

Reference: https://stackoverflow.com/a/13867319/7470786

EDIT:

For this, you should dynamically get the location of the user. After searching I came across with this answer which suggest using getTimeZoneOffset.

Rarblack
  • 4,559
  • 4
  • 22
  • 33
  • Thx. But if I upload such app online, for the other users try to retrieve their localtime, should I add the specific timezone in the setting? – Thomas.Q Dec 19 '18 at 06:49
0

You have to enable USE_TZ = True in your settings.py if you not. In order to have value in localtime in the template you can do:

{% load tz %}

{% localtime on %}
    {{ value }}
{% endlocaltime %}

Also you can set specific timezone like:

{% load tz %}

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

Or if you need one value to be converted you can do:

{% load tz %}

{{ value|localtime }}

You can read more in Django official docs.

Sergey Pugach
  • 5,561
  • 1
  • 16
  • 31
  • Thx. But if I upload such app online, for the other users try to retrieve their localtime that may be not mine, should I add the specific timezone in the setting? – Thomas.Q Dec 19 '18 at 08:36
  • If you want to have only specific timezone you can set it in settings. – Sergey Pugach Dec 19 '18 at 08:38
  • Sorry, I don't want to set specific timezone. Just let the program to identify it and show it based on the user's timezone. Can it do that? – Thomas.Q Dec 19 '18 at 08:40
0

If you want to store it in UTC and display it in the local time, then do this

 {% load tz %}

 {% timezone "Asia/Kolkata" %}
   India time: {{ value }}  #Add your respective time
 {% endtimezone %}

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

This will return UTC if no time zone is given.

Note that - Conversion to local time is not always appropriate. Also check out the warning inside Official Docs

Bidhan Majhi
  • 1,320
  • 1
  • 12
  • 25