0

I'm trying to implement the FullCalendar on my website, but am a little new to this and not quite sure how to format what I want to do. I have a view which will grab all of an individual user's events. I want to take those events and populate the calendar with them. My issue is that I don't know what to return in the view or how to handle that return value in the JavaScript function. Here's what I have right now:

View:

def calEvents(request):

    user = request.user.get_profile()
    eventList = user.eventList
    ownedList = user.ownedEvent

    events = #Part I'm having trouble with

    return HttpResponse(events)

The eventList and ownedEvent keep track of all of a user's events. They have names/dates associated with them. What I don't understand is the format I need to put all that information in to return in my HttpResponse.

My JavaScript function is:

$(document).ready(function() {


    $('#calendar').fullCalendar({
      eventSources: [
        {
            url: '/calEvents/',
            editable: false,
        }
      ]
    });

});

I'm telling it to go to the Django view, but am lost after that. Thanks so much in advance!

Xonal
  • 1,340
  • 4
  • 20
  • 33

1 Answers1

1

I have done this by building a list of dictionaries in my Django view, setting at minimum the required fields of 'title' and the start time, then using simplejson.dumps with cls=DjangoJSONEncoder to return json.

from django.core.serializers.json import DjangoJSONEncoder

def calEvents(request):
    # as above, then:
    events = []
    for event in eventList:
        events.append({'title': event.name, 'start': event.start})
    # something similar for owned events, maybe with a different className if you like
    return HttpResponse(simplejson.dumps(events, cls=DjangoJSONEncoder), mimetype='application/json')

You may also with to limit the events you return based on the starting and ending times provided by the get request:

def calEvents(request):
    user = request.user.get_profile()
    start_timestamp = request.GET.get('start')
    end_timestamp = request.GET.get('end')
    start_datetime = datetime.datetime.fromtimestamp(float(start_timestamp))
    end_datetime = datetime.datetime.fromtimestamp(float(end_timestamp))
    eventList = user.eventList.filter(start_time__lte=end_datetime, end_time__gte=start_datetime)

I am neglecting error handling for the timestamp conversion - fullcalendar will give you appropriate values, but it would be best to allow for the possibility of bad input. And I am making assumptions about the structure of your event models.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • This resulted in `TypeError: __init__() got an unexpected keyword argument 'namedtuple_as_object'`. After googling it, I saw it mentioned some backwards compatibility issues when used in Django. Is there a workaround? – Xonal Jul 30 '13 at 19:54
  • Which version of Django are you on? – Peter DeGlopper Jul 30 '13 at 19:56
  • Got it working using just json.dumps without the encoder. Thanks! – Xonal Jul 30 '13 at 20:04
  • 1
    Interesting - on my older version the default encoder couldn't handle `datetime`s, which was the motivation for using the Django specific one. The standard Python library's docs don't report it handling `datetime`s by default, so I am pleasantly surprised that it works without adding a custom encoder of some sort. – Peter DeGlopper Jul 30 '13 at 20:07
  • Oh, I will probably end up having to find one of those. At the moment, I just manually entered "2013-07-15". My goal here was just to get something to show up. I think I can figure out the kinks after that. – Xonal Jul 30 '13 at 20:09
  • 1
    http://stackoverflow.com/questions/455580/json-datetime-between-python-and-javascript like it has multiple good answers for when you do need to add it in. – Peter DeGlopper Jul 30 '13 at 20:11