Creating .ics files with django for calendar feeds

There are a lot of projects, if you are developing business software, that handle schedules/appointments. And of course, people don’t always want to use your app to sync their calendar or check their schedule.

For this reason you can create .ics-Files, that are user by the user by an URL to subscribe their calendar to new events for them, inside your app.

For Django, this is pretty easy. You can use the library django-ical to create a calendar feed. But to really get the full power of this library, we have to make some enhancements in our code.

To start you can use the following code:

This will work. The attribute item_guid is really important, because if you don’t set it, the calendar will only show the last event you added. And we want it to show all of our events.

Also try not to use related objects, if you have many events, because this will make the feed parsing really slow.

But there is a little problem right now. We would create the same calendar for every User and we can’t just add a parameter to the URL, because the ICalFeed Class does not support this.

The solution for this problem is simple. If you look at the ICalFeed-Class, you see, that it uses the dunder method __call__() and this method receives the request-parameter.

We can use this, to override the method in our child-class to get the request parameter:

And that’s it. You can now use the request object in the items-method to filter or do whatever you want.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.