Python: Datetime countdown
I'm having problem with countdown. This is the code I use, and it's just show a static countdown, it will only change if I refresh the page. I want it to be like this, 0d 0h 1m 3s, 0d 0h 1m 2s and so on. I tried using for i in range, it seems like it only accepts integer? But my variable is date so it shows error. How can I make this possible? Thank you in advance!
views.py
def dateDiffInSeconds(date1, date2):
timedelta = date2 - date1
return timedelta.days * 24 * 3600 + timedelta.seconds
def daysHoursMinutesSecondsFromSeconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
def registration(request)
access = WebsiteSettings.objects.all()
now = datetime.datetime.today().replace(tzinfo=pytz.UTC)
for item in access:
countdown = "%dd %dh %dm %ds" % daysHoursMinutesSecondsFromSeconds(dateDiffInSeconds(now, item.date_open))
sleep(1)
return render(request, 'registration.html', {'countdown': countdown})