[Answer]-Using FullCalendar with Django Views

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.

Leave a comment