我可以通过修改的HTMLCalendar()从我的Django模型迭代对象吗?

时间:2011-09-02 15:46:14

标签: python django

此代码仅从数据库中检索单个对象,而不是在适用日期的整个日历中迭代它们。

cal.py

from www.wednesday.models import Event
import calendar

class EventCal(calendar.HTMLCalendar):         
    def formatday(self, day, weekday):     
        if day == 0:
            return '<td class="noday">&nbsp;</td>' # Day outside month
        if day == int(event.dateDay.day):
            return '<td class="%s">%d</p><a href=\"%s\" target=\"_blank\">%s</a></td>' % (self.cssclasses[weekday], day, event.linkURL, event.restaurant)
        else:
            return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day)

events = Event.objects.all()
for event in events:
    class rendCal():
        c = EventCal(calendar.SUNDAY)

views.py

from django.shortcuts import render_to_response
from www.wednesday.models import Event
from www.wednesday.cal import *
import datetime as dt

Months = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

now = dt.datetime.now()
mon = now.month

def calend(request):
    cal = rendCal.c.formatmonth(now.year, now.month)
    events = Event.objects.all()
    return render_to_response('cal.html', {'calendar': cal,
                                           'title': 'Win-Win Wednesday Calendar for %s' % Months[int(mon)]})

cal.html

# ...
<div id='calendar'>

<div>
</div>
{% block main %}
{{ calendar|safe }}
{% endblock %}
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

你的大部分问题都是模糊的。

  

而且,它打印的是django标签{{event.restaurant}},而不是模型中的数据。

那是因为formatday()返回字符串"{{ event.restaurant }}"

模板渲染将该字符串插入模板中。

模板渲染不会查看它插入的数据,以查看数据是否是更多模板代码。它只是将数据插入模板中。