Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the next seven days of the week in views – Django

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'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading