Home > Net >  Get the next seven days of the week in views - Django
Get the next seven days of the week in views - Django

Time:02-10

How to get seven consecutive days of the week in views using Django. I try this, but it returns an error.

from datetime import datetime

day = datetime.now()
day_1 = datetime.now()   datetime.timedelta(days=1)
day_2 = datetime.now()   datetime.timedelta(days=2)
day_3 = datetime.now()   datetime.timedelta(days=3)
day_4 = datetime.now()   datetime.timedelta(days=4)
day_5 = datetime.now()   datetime.timedelta(days=5)
day_6 = datetime.now()   datetime.timedelta(days=6)
day_7 = datetime.now()   datetime.timedelta(days=7)

It returns:

type object 'datetime.datetime' has no attribute 'timedelta'

CodePudding user response:

The datetime in datetime.timedelta is about the module not the class, since you here import the class, you can also import timedelta directly:

from datetime import datetime, timedelta

day = datetime.now()
day1 = day   timedelta(days=1)
day2 = day   timedelta(days=2)
day3 = day   timedelta(days=3)
day4 = day   timedelta(days=4)
day5 = day   timedelta(days=5)
day6 = day   timedelta(days=6)
day7 = day   timedelta(days=7)

CodePudding user response:

import timedelta from datetime and use directly as

from datetime import datetime, timedelta

day = datetime.now()
day_1 = datetime.now()   timedelta(days=1)
day_2 = datetime.now()   timedelta(days=2)
day_3 = datetime.now()   timedelta(days=3)
day_4 = datetime.now()   timedelta(days=4)
day_5 = datetime.now()   timedelta(days=5)
day_6 = datetime.now()   timedelta(days=6)
day_7 = datetime.now()   timedelta(days=7)
  • Related