Reputation: 8101
I want to use fullcalendar jquery plugin in my django project. I have an Entry model
class Entry(models.Model):
date = models.DatetimeField()
it only has a date attribute. I know that fullcalendar uses start and end but will get to that too :)
my ajax view is the following
def eventsFeed(request):
from django.utils.timezone import utc
from django.core.serializers.json import DjangoJSONEncoder
if request.is_ajax():
print 'Its ajax from fullCalendar()'
try:
start = datetime.fromtimestamp(int(request.GET.get('start', False))).replace(tzinfo=utc)
end = datetime.fromtimestamp(int(request.GET.get('end',False)))
except ValueError:
start = datetime.now.replace(tzinfo=utc)
end = start + timedelta(days=7)
entries = Entry.objects.filter(date__gte=start).filter(date__lte=end)
print entries
json_list = []
for entry in entries:
id = entry.id
title = entry.title
start = entry.date.strftime("%Y-%m-%dT%H:%M:%S")
allDay = False
json_entry = {'id':id, 'start':start, 'allDay':allDay, 'title': title}
json_entry = json.dumps(json_entry, cls=DjangoJSONEncoder)
json_list.append(json_entry)
return HttpResponse(json_list, content_type='application/json')
my template.html
<div id="calendar"></div>
<script>
$(document).ready(function (){
$("#calendar").fullCalendar({
events:"calendar/eventfeeds/"
});
});
i have to objects for tests right now in my db all at todays date and four more in former days. Also i have timezone enabled so my models are storing aware datetime objects
But fullcalendar doesn't show events. Full Calendar starts from todays date (25th of november). So what i get is
start, end = 2013-10-27 00:00:00+00:00, 2013-12-08 00:00:00
(why is that since i got the november view?)
[<Entry: doctor - Title>, <Entry: doctor - New entry>, <Entry: doctor - Entry3>,
, , ]
normally it gets all entries since the start and end dates are not the ones they supposed to be(remember November view)
and the json_list
['{"start": "2013-11-20T17:30:00", "allDay": false, "id": 1, "title": "Title"}',
'{"start": "2013-11-20T17:30:00", "allDay": false, "id": 2, "title": "New entry
"}', '{"start": "2013-11-20T17:30:00", "allDay": false, "id": 3, "title": "Entry
3"}', '{"start": "2013-11-21T22:00:00", "allDay": false, "id": 4, "title": "test
"}', '{"start": "2013-11-25T13:00:00", "allDay": false, "id": 5, "title": "New e
ntry"}', '{"start": "2013-11-25T09:52:00", "allDay": false, "id": 6, "title": "N
ew entry 2"}']
Changed start date to be in ISO8601 format. As for end date it says in documentation that if event object doesn't have enddate it uses end date as 120 minutes after start date. So it is auto populated. But events don't render...So what am i doing wrong?
EDIT1: going to calendar/eventsfeed/ I get an empty page. Meaning json objects are not returned....?? EDIT2: I was wrong, eventfeeds does return json normally, I just didn't pass any GET params when checked it in EDIT1
Upvotes: 3
Views: 4716
Reputation: 8101
Solved it. I guess fullcalendar wanted a list with json objects, but i was returning plain json objects. What I did was
for entry in entries:
id = entry.id
title = entry.title
start = entry.date.strftime("%Y-%m-%dT%H:%M:%S")
allDay = False
json_entry = {'id':id, 'start':start, 'allDay':allDay, 'title': title}
json_list.append(json_entry)
return HttpResponse(json.dumps(json_list), content_type='application/json')
I didn't json.dumpbed every json_entry but only one time the json_list. And worked like a charm. Thank you all
Upvotes: 7