Oli Warner About Contact Oli on Twitter Subscribe

Why I love Disqus

Thursday, 16 September 2010 django webdev

Once upon a time if I wanted comments on a website, I would do them myself, tediously tackling problems solved a dozen times before to deliver an intuitive system. But today is a new day and I wouldn’t dream of writing comment code unless I really had to.

An essential part of any blogging engine is allowing your readers to give you feedback and create discussions around a topic. There are few things worse than landing on a blog, reading some tripe and not being able to flame the writer for it.

In the dozen-or-so iterations of ThePCSpy.com, I’ve gone through four major home-made blogging engines: the first two in classic ASP, the second in some very clever, very verbose ASP.NET and the latest in Django.

Before the current implementation, each engine needed me to implement a comments feature. The first two were basic and the ASP.NET version was several hundred lines long with clever sorting, nesting, editing and AJAX. When I moved to Django I had three choices:

  1. Re-re-re-re-re-reinvent the wheel, bringing everything I had learned to Python/Django,
  2. Use a pre-made Django app like django-threadedcomments and create a nice interface for people
  3. Use something off-site, like Disqus

Both #1 and #2 require serious, ongoing work

Doing comments well requires an authentication system (that I had implemented in previous iterations) which, in turn, requires registration, forgotten password, login and logout pages. Django does most of this very simply but then you have to hook that into your comments to allow a dual-system of registered and anonymous comments. It’s a problem I had solved for the ASP.NET version but it was ugly and as I said, hundreds of lines of logic that could not be easily ported to Python.

Then there are features like subscriptions, moderation, banning users… Things you forget about when you first dive into writing your own comments section. Things that take even more time and even more logic.

And then there’s performance. Showing comments, pulling up user details, getting the nesting and paging done correctly, implementing the AJAX to run effectively, partial caching of the page so comments update immediately without having to regenerate the main page, optimising SQL queries… All things that take serious consideration and design to get right.

At this point I started to get a headache. I didn’t want to build something from scratch or customise something to death. I just wanted comments on the new site so I had a look at option #3. I looked at Disqus and IntenseDebate. I already had an account with both from commenting on other people’s blogs but I decided on a coin-flip and ended up with Disqus, itself a Django-powered service.

Implementing it was beyond simple. After setting up the site settings on Disqus, I added this to my posts’ template:

<div id="disqus_thread"></div>
<script type="text/javascript">
centreAll();
disqus_url = "https://thepcspy.com{{ request.path }}";
disqus_title = "{{ post.title | safe }}";
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript';
dsq.async = true;
dsq.src = 'http://tpcs.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>

Refreshed the page and I could add new comments to the blog.

Porting the old comments was slightly harder

When I dumped my old ASP.NET site, all I had of the MS SQL Server database was JSON dumps of the posts and comments. I wrote a little script to import both, posts into my new BlogPost model and comments into django.contrib.comments. This meant I now had all the data available from within Django but I needed to upload the comment data to Disqus.

Thankfully there’s another app called django-disqus that really helps here. It has a manage.py function that allows you to export django.contrib.comments posts to Disqus so I installed it and ran ./manage.py disqus_export.

After a few botched attempts and then a few hours it had uploaded all the comments on all the old posts and I removed both django.contrib.comments and django-disqus as neither were needed anymore.

Now I have:

All for free with a tiny bit of effort.

There are some downsides

The most evident is that if Disqus goes down, shuts down, gets hacked, etc, it effects me. I have to have a certain amount of trust.

I also don’t have any direct handle on the comments from the blog engine. This makes it very hard to rank posts by comment quantity or recent comments. If I wanted to access the numbers, it would have to query the Disqus API and that’s something I didn’t want to get entrenched in. Thankfully Discus provide javascript tools that you can plonk on a page and forget about.

Any off-site comment solution is also going to be useless if you needed to control who was posting. In this cases you probably already have user authentication so you’d want to use something that hooked directly into that database.

But all-in-all, a great result

For the purposes of blogging, I have only compliments for Disqus. It has saved me several days of legwork, countless gigabytes of bandwidth, hours of CPU time and provides me (and you, my readers) a better commenting experience.

If you’re writing your own blog (or even if you’re using something like Wordpress), I’d strongly consider getting your comments on Disqus (or other) and giving yourself and your server a well earned break.