我正在使用Django创建日历应用。日历必须是垂直的

时间:2019-10-22 04:56:40

标签: python html django python-3.x calendar

到目前为止,我已经意识到了这个django-calendar。但是我需要vertical calendar(对不起,还没有找到英语)在左侧,我们在顶部有工作日标题和月份名称。

我开始重写python-calendar HTMLCalendar方法。我想写一种新的formatweekdayrow方法,该方法将生成星期一,星期二....星期日的行。但由于无法完全理解所有这些方法的工作原理,我陷入了困境。

所以问题是我应该考虑写格式格式daydayrow的想法,还是不能制作这样的垂直日历?还是可能有一些更简单,更聪明的方法来做到这一点?

1 个答案:

答案 0 :(得分:0)

我重写了几种方法并得到了我需要的

class Vertical(HTMLCalendar):

    def formatweekrow(self, theweeks, daynum):
        """
        Return a complete week as a table row.
        """
        v = []
        s = v.append
        s('<tr>')
        for theweek in theweeks:
            for (d, wd) in theweek:
                if wd == daynum:
                    s(self.formatday(d, wd))
        s('</tr>')
        return ''.join(v)

    def formatmonth(self, theyear, themonth, withyear=True):
        """
        Return a formatted month as a table.
        """
        cnt = 0
        v = []
        theweeks = self.monthdays2calendar(theyear, themonth)
        a = v.append
        a('<table border="0" cellpadding="0" cellspacing="0"     class="">')
        a('\n')
        a(self.formatmonthname(theyear, themonth, withyear=withyear))
        a('\n')
        a(self.formatweekheader())
        a('\n')
        while cnt < 7:
            a(self.formatweekrow(theweeks, cnt))
            a('\n')
            cnt += 1
        a('</table>')
        a('\n')
        return ''.join(v)