Reputation: 15
I am developing a website using django framework and i am kinda new to it. I want to display an item from the database for a specific time on the webpage. I have a custom filter to implement the timer logic , however I am trying to understand that how can I display the actual timer on the page.
Below is the logic for timer :-
@register.filter(name="time_left")
def time_left(value):
t = value - timezone.now()
days, seconds = t.days, t.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
st = str(minutes) + "m " + str(seconds) + "s"
return st
Upvotes: 0
Views: 359
Reputation: 759
I think you are almost complete with your logic. you just have to pass it to the variable.
views.py
@register.filter(name="time_left")
def time_left(value):
t = value - timezone.now()
days, seconds = t.days, t.seconds
hours = days * 24 + seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
st = str(minutes) + "m " + str(seconds) + "s"
context = {"st":st}
return render(request, "template/page.html", context)
urls.py
from .views import time_left
urlpatterns = [
path('', time_left, name="timeleft")
]
page.html
The Remaining time is {{st}}
Upvotes: 1