It's nice to have a calendar that displays all the days in a month, but sometimes its handy to also display the extra days padding the month so you don't end up with blank spots.
Left has no padding. Right has padding dates in grey.
I like to have more information when possible. So, to get those extra days you just have to use a simple method already built into the calendar system.
1.
from
calendar
import
Calendar, SUNDAY
def
generate_date_range(year, month):
# Process the calendar month to get the proper start/end dates
2.
cal
=
Calendar(SUNDAY)
# Make Sunday first day of the week
3.
days
=
[ day
for
day
in
cal.itermonthdates(year, month) ]
4.
return
days
Another thing you may find helpful is to split the days into weeks for display in rows.
1.
def
split_days_to_weeks(days):
2.
weeks
=
[]
3.
4.
for
i
in
range((len(days)
/
7
)
+
1
):
5.
weeks.append(days[i
*
7
: (i
+
1
)
*
7
])
6.
7.
return
weeks
[ Source ]