【发布时间】:2011-07-25 17:50:48
【问题描述】:
我正在尝试使用 django 构建一个日历视图,并试图避免在我的视图中写入任何 html 标签。我找到了一个帮助我到达那里的代码,
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
在我看来,我做了以下事情
<div class="calendar_panel_bottom_noborder">
<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>
现在,上面的代码正在根据月份和年份参数打印日历,但它们看起来是错误的。当我将它与日历进行比较时,使用 2011 年 12 月,该月的第一天从星期四开始,而在日历中,它从星期三开始。谁能帮忙指出我在这里做错了什么?
更新
问题似乎来自 itermonthdays。我尝试执行以下操作
>>> 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
我不知道我是否应该更改某种设置或以不同的方式启动课程。但是,根据我的 PC 日历,输出应以 4 个零开头,以无结尾,它显示 2011 年 12 月从一周的第 5 天的星期四开始,因此在计数开始之前应该有 4 个零。嗯,对我如何解决这个问题有什么建议吗?
【问题讨论】:
-
没有远程连接问题,但是你为什么用
<div>和<span>来表示表格数据? -
我有一个 CSS 类来处理表示。它显示输出一个表格,