New Comment Notification by E-mail

This little trick certainly isn't anything special. Several other folks have posted about how to have Django send out an e-mail automagically when a new comment has been posted on your site. However, a lot of these other posts seem to be pre-Django 1.0. Some groovy changes took place with the signal system slightly before Django 1.0 was released, so I thought I'd share my method of having Django notify me by e-mail when someone posts a comment on my sites.

My code follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from django.db.models.signals import post_save
from django.core.mail import mail_admins
from django.contrib.comments.models import Comment

def notify_of_comment(sender, instance, **kwargs):
    message = 'A new comment has been posted.\n'
    message += instance.get_as_text()

    mail_admins('New Comment', message)

post_save.connect(notify_of_comment, sender=Comment)

Nice and simple, right? Right. All this does is it creates a callback function called notify_of_comment and waits for Django to signal that a Comment object has been saved. Any time that signal is intercepted, the callback will send off an e-mail to anyone in the settings.ADMINS list.

You should be able to put such code anywhere in your project so long as that anywhere is loaded when your project starts up. I usually put signal interceptors in an __init__.py file somewhere, though they should work just as well in a models.py or urls.py file.

I realize that the django-comment-utils project is capable of notifying when a comment has been posted, but I didn't want much else from the project. That is why I did it this way ;)

Meta

Published: Nov. 30, 2008

Author: Josh VanderLinden

Comments: 4

Word Count: 262

Next: django-watermark 0.1.2-pre1

Previous: Adding Captcha To Django's Built-in Comments

Bookmark and Share

Filed Under

Django, How To, Open Source, Programming, and Python

Comments

Gravatar for None
Sam McDonald
Thanks for posting this, comment notifications are about 2 items down on my todo list, so this has been very helpful.
30 Nov. 2008 at 10:37 a.m.
Gravatar for codekoala
wheaties
Glad to help!!
30 Nov. 2008 at 10:39 a.m.
Gravatar for None
Marc Fargas
You may like to know that in kwargs there's a "created" argument that is set to True if the saved item was Inserted into the DB (aka: Is new).

You may check it to make sure you do not notify a comment if you edit it ;)
1 Dec. 2008 at 3:08 a.m.
Gravatar for codekoala
wheaties
Yup. The version I'm using actually takes that into account, but for the sake of simplicity I didn't include it in this snippet. I still want to know when comments are updated, so I change some of the wording in the message I get.
1 Dec. 2008 at 6:20 a.m.

Post a Comment

:  
:  
:  
:  
:  

About Me

My name is Josh VanderLinden. I'm a developer, and I have a passion for technology. I dream in code.

I use Linux, Kate, Java, Python, Django, databases, Photoshop, and Firefox.

About This Site

CodeKoala.com was developed using Django and Python, and it's bursting at the seams with love.

You're one of 11 active visitors. One is viewing this very page.

Recently Finished
Books I'm Reading

None at this time.

I have read approximately 7,874 pages since September 1, 2008.

Books I Plan To Read