How do I create number of days to go progress bar on django

145 Views Asked by At

Please I've an App I created, and I'm finding it hard to add progress bar count down for days to go before the user get what they Applied for? I used the formula now - start time / end time - now + now - start time all in days, seems to work pretty well but things get awkward when the month number changes... Please any help

1

There are 1 best solutions below

0
ben On

No need for convoluted formulas. Use datetime objects and just subtract the current time from the start time. E.g.

# models.py
from django.db import models
from django.utils.timezone import now


class Event(models.Model):
    event_name = models.CharField(max_length=50)
    start_time = models.DateTimeField(auto_now=False, auto_now_add=False)

    @property
    def days_to_go(self):
        time_remaining = self.start_time - now()
        return abs(time_remaining.days)

days_to_go will now be available in the context object, along with event_name and start_time

See here to get a better understanding of working with time in Django.

See here for Django's DateTimeField model field.