简体   繁体   English

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

[英]Can I iterate objects from my Django model over a modified HTMLCalendar()?

This code will only retrieve a single object from the database, rather than iterate them throughout the calendar on their applicable date. 此代码将仅从数据库中检索单个对象,而不在适用日期的整个日历中对其进行迭代。

cal.py 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 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 cal.html

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

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

Most of your question is murky. 您的大部分问题都是模糊的。

And, it's printing the django tag {{ event.restaurant }} rather than the data from the model. 而且,它正在打印django标签{{event.restaurant}},而不是模型中的数据。

That's because formatday() returns the string "{{ event.restaurant }}" . 这是因为formatday()返回字符串"{{ event.restaurant }}"

The template rendering inserts that string into the template. 模板渲染会将字符串插入模板。

The template rendering doesn't look at the data it inserts to see if the data is more template code. 模板渲染不会查看它插入的数据,而是查看数据是否是更多的模板代码。 It just inserts data into the template. 它只是将数据插入模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM