Django buit-in comments allows customization. You will need to do this when:
- You want to add more model field.
- You want to customize comments form, add some checking or validating.
- You want to customize the comment views
I want to integrate captcha into the django built-in comment to prevent comment's spamming. The goal is no hack or modify something inside the comment and a captcha application itself.
The idea is, I will customize the comment's form by adding the captcha field. That's all!
The captcha application
I use django-simple-captcha. It provides the captcha field so that I can easily use with the my comment's form. it also has a set of features, allow me to control how the captcha will look by set the settings.
To install django-simple-captcha, check out the svn
Create your custom comment applications
To customize the built-in comment, I create new application name custom_comments.
Extend built-in comment form
At the forms.py of custom_comments application, I have the following code,
from django import forms from django.contrib.comments.forms import CommentForm from captcha.fields import CaptchaField class CommentFormWithCaptcha(CommentForm): captcha = CaptchaField() def get_comment_create_data(self): return super(CommentFormWithCaptcha, self).get_comment_create_data()
To enable above code, I put the following code at the _init_.py of custom_comments'.
from custom_comments.forms import CommentFormWithCaptcha def get_form(): return CommentFormWithCaptcha
Tell django to use your comment app
I put the "custom_comments" into the installed application setting and set the COMMENTS_APP to "custom_comments".
INSTALLED_APPS = ( .... 'django.contrib.comments', .... 'custom_comments', ) COMMENTS_APP = 'custom_comments'
That's all. Form now all, my comment form will have a chapcha.
More about django-simple-captcha
The django-simple-captcha has following settings to allow controlling of the captcha.
CAPTCHA_FONT_PATH = getattr(settings,'CAPTCHA_FONT_PATH', os.path.normpath(os.path.join(os.path.dirname(__file__), '..', 'fonts/etl24-unicode.pil'))) CAPTCHA_FONT_SIZE = getattr(settings,'CAPTCHA_FONT_SIZE', 22) CAPTCHA_LETTER_ROTATION = getattr(settings, 'CAPTCHA_LETTER_ROTATION', (-35,35)) CAPTCHA_BACKGROUND_COLOR = getattr(settings,'CAPTCHA_BACKGROUND_COLOR', '#ffffff') CAPTCHA_FOREGROUND_COLOR= getattr(settings,'CAPTCHA_FOREGROUND_COLOR', '#001100') CAPTCHA_CHALLENGE_FUNCT = getattr(settings,'CAPTCHA_CHALLENGE_FUNCT','captcha.helpers.random_char_challenge') CAPTCHA_NOISE_FUNCTIONS = getattr(settings,'CAPTCHA_NOISE_FUNCTIONS', ('captcha.helpers.noise_arcs','captcha.helpers.noise_dots',)) CAPTCHA_FILTER_FUNCTIONS = getattr(settings,'CAPTCHA_FILTER_FUNCTIONS',('captcha.helpers.post_smooth',)) CAPTCHA_WORDS_DICTIONARY = getattr(settings,'CAPTCHA_WORDS_DICTIONARY', '/usr/share/dict/words') CAPTCHA_FLITE_PATH = getattr(settings,'CAPTCHA_FLITE_PATH',None) CAPTCHA_TIMEOUT = getattr(settings, 'CAPTCHA_TIMEOUT', 5) # Minutes CAPTCHA_LENGTH = int(getattr(settings, 'CAPTCHA_LENGTH', 5)) # Chars
Comments
# ZK@Web Marketing Blog Jul 1st, 2009
I admit, I have not been on this webpage in a long time... however it was another joy to see It is such an important topic and ignored by so many, even professionals. I thank you to help making people more aware of possible issues. Great stuff as usual....
# Online Glücksspiele Jul 1st, 2009
I really like what they've done and they've made it easy to add comments to just about anything. One thing however that nagged me from the beginning was their method of "spam protection." Their idea of spam protection was to simply include a "honeypot" field that a spammer would fill out along with all the other fields. Upon submission, if there is any data in that field, the submission would fail. That's all well an good, but say you want to up the ante just a bit and add some form of captcha? Since we're all lazy programmers, why not implement reCaptcha (recaptcha.net)?
# wrong Jul 20th, 2009
Thank you.
# djm Aug 23rd, 2009
@ Online Glücksspiele.
I agree; what's more annoying is that the honeypot field is actually named "honeypot". I mean cmon, doesn't take the greatest spam bot in the world to work that one out.
# http://www.promozionivirtuale.com/ Oct 22nd, 2009
I really appreciate your work to this site.So continue this kind of stuff in future also.It helps me lot.
# sharmawebsolution Dec 4th, 2009
We provide you all Seo,Hosting and including top web marketing concepts like that– website development, Internet Marketing,Search Engine Optimization.
# alternative medicines Dec 20th, 2009
WE PUT MANY TIME IN A SITE CORRECT captcha BUT IT NOT WORKING CAN I KNOW WHAT'S PROBLEM
# ppo plans Jan 27th, 2010
Django Simple Captcha is an extremely simple, yet highly customizable Django application to add captcha images to any Django form
# Michael Mar 10th, 2010
What do you think, is there any way to use the "CommentFormWithCaptcha" only in case of unregistered users and the default Form in case of registered users? I have hard time figuring out if it is possible with the Django's comment system.
# meledictas Mar 11th, 2010
@Michael, I think you should do that at view level or template tag that look at request and dynamically create form. For example,
{% get_comment_form request %}
Hope that help.