Grant Gubatan
Grant Gubatan

Reputation: 265

DJANGO Different Timezone Display

Django Beginner here. I'm trying to display different timezones in my template but it's not displaying.

HTML File

  {% load tz %}

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

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

OUTPUT

No Display in HTML

Upvotes: 0

Views: 444

Answers (2)

kimbo
kimbo

Reputation: 2703

I'm guessing you're not getting anything displayed because you haven't passed any context to your template. You would do something like this:

    from django.utils import timezone
    ...
    return render(request, template, context={'value': timezone.now()})

Another option is to use django's built-in {% now &} with whatever format you want.

Similar question: Django - present current date and time in template

Now docs: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#now

Date filters to include with now: https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#std:templatefilter-date

Upvotes: 1

strang1ato
strang1ato

Reputation: 37

In setting.py change TIME_ZONE:

TIME_ZONE = 'Europe/Paris'

views.py should look like this:

from django.shortcuts import render
import datetime
from django.utils import timezone

def index(request):
    france = timezone.localtime(timezone.now())
    now = datetime.datetime.now()

    return render(request, 'home/home.html', {'now':now, 'france': france} )

And home.html should look like this:

France time: {{france}}
Server time: {{ now }}

It should work

Upvotes: 2

Related Questions