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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
Which version of Django are you on?
Got it working using just json.dumps without the encoder. Thanks!
Interesting - on my older version the default encoder couldn't handle datetimes, which was the motivation for using the Django specific one. The standard Python library's docs don't report it handling datetimes by default, so I am pleasantly surprised that it works without adding a custom encoder of some sort.
stackoverflow.com/questions/455580/… like it has multiple good answers for when you do need to add it in.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.