简体   繁体   中英

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

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

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

# 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM