Blog Spam Protection

I currently manually review and delete comment spam every few days. It doesn't take long, it's just frustrating.

When you regularly repeat an action over a long period of time, you begin to notice patterns of action. What I've noticed is that most comment spam is being posted to older blog posts - most likely due to their higher number of in-links, and I'm guessing higher google page rank.

To this end, what I've done is add a method to my blog Post model, which checks the post date and, if it's within the defined timeframe, will return 'True'. My Post detail template now checks this method and only displays the form when appropriate.

The first file to change is your blog applications At the top of the file add this import statement to bring in the 'datetime' module and required tools:

from datetime import datetime, timedelta

Next, in the same file, add the following method to your 'Post' class (just after the 'body' field is fine):

    def allow_comments(self):
        return ( + timedelta(21)) >

Lastly, open up your blog detail template and replace the existing call to {% freecomment_form ... %}_ with:

{% if object.allow_comments %}
{% free_comment_form for %}
{% else %}
<p>Comments can only be posted for recent articles.</p>
{% endif %}

What this does is pretty straightforward. Using datetime and timedelta objects, we ensure that only posts within the last 21 days can have comments posted to them. For me, this timespan works well, you may need to experiment with longer or shorter times.

Next up for me is actually altering the 'comments' mechanism to add filtering via Akismet or similar processes - but I'm wary of modifying the contrib code as I dread upgrade-time!

Thanks for reading.

I'd love to hear your feedback and comments. E-mail me,, or get in touch on Twitter where I'm @RossPoulton. Chat soon!