0

I am trying to create a face detection program that will detect a face and log the time it was detected in. I created a Django template and am trying to display the time on the template. But the time it is displaying is wrong.

To double check, I tried printing the current time separately in a different python program. That displays the time correctly.

This is code for displaying the time in the Django template (info.html)

views.py

import datetime

def detect(request):

    global now
    now = datetime.datetime.now()

def info(request):
    return render(request, 'info.html', {'entry_time':now})

this is code I used in the separate python file

import datetime

print(datetime.datetime.now())

both the code is the same. I checked online and found this links How to get the current time in Python

I also tried setting the timezone to my timezone (Asia/Kolkata) according to this link Is there a list of Pytz Timezones?

I feel that I am not declaring the 'now' variable in the right place. Please help me out. I am new to Django

Thanks in advance

  • 1
    What is the timezone in your `settings.py` and what is your actual timezone? – Selcuk Aug 01 '18 at 04:15
  • have you tried with django's `timezone.now()`, please refer this https://docs.djangoproject.com/en/2.0/topics/i18n/timezones/ – Nakul Narayanan Aug 01 '18 at 04:22
  • I changed the timezone in settings.py to TIME_ZONE = 'Asia/Kolkata'. This is my actual timezone. –  Aug 01 '18 at 04:29

1 Answers1

1

In your settings.py change TIME_ZONE variable as you like.

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True
Ali
  • 2,541
  • 2
  • 17
  • 31
  • I did that. But had to terminate the session and switch it bak again to see the changes. Thanks a lot. –  Aug 01 '18 at 04:36