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
from www.wednesday.models import Event
import calendar
class EventCal(calendar.HTMLCalendar):
def formatday(self, day, weekday):
if day == 0:
return '<td class="noday"> </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 i开发者_JAVA百科mport *
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>
Most of your question is murky.
And, it's printing the django tag {{ event.restaurant }} rather than the data from the model.
That's because formatday()
returns the string "{{ 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.
精彩评论