I am trying to build a calendar view using django and trying to avoid writing any html tags inside my view. I found a code that helped me get there,
from datetime import date, datetime, timedelta, time
import calendar
year=2011
month=12
change=None
# init variables
cal = calendar.Calendar()
month_days = cal.itermonthdays(year, month)
lst = [[]]
week = 0
# make month lists containing list of days for each week
# each day tuple will contain list of entries and 'current' indicator
for day in month_days:
lst[week].append((day))
if len(lst[week]) == 7:
lst.append([])
week += 1
and in my view, i did the following
<div class="calendar_panel_bottom_no开发者_JS百科border">
<span class="month">Juny 2011</span>
<span class="day_name">S</span>
<span class="day_name">M</span>
<span class="day_name">T</span>
<span class="day_name">W</span>
<span class="day_name">T</span>
<span class="day_name">F</span>
<span class="day_name">S</span>
{% for week in month_days %}
{% for day in week %}
<span class="date">{{ day }}</span>
{% endfor %}
{% endfor %}
<div class="clear"></div>
</div>
Now, the above code is printing the calendar as per the month and year parameters but they are appearing wrong. When I compare it with the calendar, using december 2011, the first day of the month starts at thursday while in calendar its starting on wednesday. can anyone help point out what I did wrong here?
UPDATE
It seems like the problem is coming from itermonthdays. I tried to do the following
>>> month_days = cal.itermonthdays(2011, 12)
>>> for day in month_days:
... print day
...
0
0
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
I don't know if I should change some sort of a settings or initiate the class differently. But, the output should start with 4 zeros and end with none based on my PC calendar it shows that december 2011 starts on a thursday which is the 5th day of the week therefore there should be 4 zeros before the count starts. hmm any advice on how i can tackle this issue?
You need to include
cal.setfirstweekday(6)
before
monthdays = ...
According to python documentation, ...
By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention).
It looks to me like your problem is simply that itermonthdays
starts with Monday instead of Sunday. You can confirm this by temporarily replacing
itermonthdays
with
itermonthdates
and
lst[week].append(day)
with
lst[week].append(day.strftime("%A")))
This should temporarily replace day numbers with day of the week names.
Edit: One possible solution to your problem is to initialize lst
to [[0]]
and then set lst=lst[:-1]
at the end of your loop.
When I compare it with the calendar, using december 2011, the first day of the month starts at thursday while in calendar its starting on wednesday. can anyone help point out what I did wrong here?
I have bad new for you, the first day of december 2011 is thursday, according my windows calendar
I recently made a calendar to display latest month days
views.py
def index(request):
x=str(datetime.now())
d0 = datetime(year=date.fromisoformat(x.split(' ')[0]).year, month=date.fromisoformat(x.split(' ')[0]).month, day=1)
d1 = datetime(year=date.fromisoformat(x.split(' ')[0]).year, month=date.fromisoformat(x.split(' ')[0]).month+1, day=1)
cal = Calendar()
lst = [[]]
week = 0
cal.setfirstweekday(0)
monthly_days = cal.itermonthdates(date.fromisoformat(x.split(' ')[0]).year, date.fromisoformat(x.split(' ')[0]).month)
month1=date.fromisoformat(x.split(' ')[0]).month
year1=date.fromisoformat(x.split(' ')[0]).year
for day in monthly_days:
if len(lst[week])<7:
if int(day.strftime('%m'))!=int(str(month1)):
lst[week].append(' ')
else:
lst[week].append(day.strftime('%d'))
else:
week+=1
if int(day.strftime('%m'))!=int(str(month1)):
lst.append([' '])
else:
lst.append([day.strftime('%d')])
return render(request,'base.html',{'monthly_days':lst})
base.html
<body>
<h1>
Hi!!!!
</h1>
<h2>
Welcome to Calendar
</h2>
<div>
<table>
<tr>
<td><span class="day_name">M</span></td>
<td><span class="day_name">T</span></td>
<td><span class="day_name">W</span></td>
<td><span class="day_name">T</span></td>
<td><span class="day_name">F</span></td>
<td><span class="day_name">S</span></td>
<td><span class="day_name">S</span></td>
</tr>
{% for week in monthly_days %}
<tr>
{% for day in week %}
<td><span class="date">{{ day }}</span></td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
</body>
This is for a tabular format
精彩评论