简体   繁体   中英

how to set a timezone.now on django

Hello i'm strugling trying to set a variable with a date.today. I don't want to set an auto_now i want to set a variable with the date of the access of the user.

i'm not quite shure if i should create a Field with an function by default or just set a variable on views.py i've tried both and i'm getting lost

models.py:

    task = models.CharField(max_length=150)
    topic = models.CharField(max_length=150)
    how = models.TextField(max_length=600)
    start = models.DateField(blank=False, auto_now_add=True)
    end = models.DateField(blank=False)
    updated_at = models.DateTimeField(auto_now=True)

views.py:

class DetailView(DetailView):
    template_name = 'details.html'
    model = To_do
    now = datetime.today

    def get_queryset(self):
        return To_do.objects.annotate(
            delta2=ExpressionWrapper(F('end') - F('now'), output_field=DurationField()))

i want to get the today date to display a countdown (end - now) every time the user open the page

Use This

from datetime import datetime
then = datetime(2019, 8, 1, 23, 8, 15)        # Random date in the past
now  = datetime.now()                         # Now
duration = now - then                         # For build-in functions
days_left = int(duration.total_seconds()/86400)   
print(days_left)

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