I really need an answer to this question. Why? Because I was, until recently, on the verge of shutting down the comments on the site because of the load of blog spam that I was receiving. It was insufferable. But, luckily, Django came to my rescue again and made a potential solution very pain-free – except for one problem.
My friend Josh introduced me to a good little captcha for Django: Captcha for Django 1.1. There are several other captchas out there: Djaptcha, MathCaptchaForm and some good tutorials. But, I like this captcha solution for a couple reasons: - Josh suggested it, and let’s face it, that carries a lot of weight. ;) - This solution jives well with ModelForm, which I use for all of my comment forms. Most of the other code that I found was for forms.Form. - This solution favors composition over inheritance: It is a form field instead of a subclass form type.
A short explanation of the file is available here.
The explanation prompts you to unload the tar into the django.contrib directory. I’m thinking this is probably how things used to jive in Django Land, but I didn’t put it there and just put it in my project. site-packages might have been a better place.
You can attach the captcha field to any model form by just including it in the class:
1 2 3 4 5 6 7 8 9 10
Also seen are just a few of the many options for the CaptchaField. You can apply these for every captcha field you write or globally in settings.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
When I implemented this for aprilandjake.com, Josh helped me out and showed me where the code needed to be tweaked: On line 174:
was changed to
Voila! It worked like a charm.
But then I uploaded it to my server and tried it out – no dice. I couldn’t figure it out. A couple days later, I was determined to find the problem… After a frustrating while and some good ‘ol debugging print statements, I determined that the problem was that because I put my project under version control and didn’t bother to remove .svn folders from production, they were being pulled in by the captcha code, thinking it was an option for a font in the ‘fonts’ subdirectory of the module. To get around the problem, I needed to change the code around line 110 from:
1 2 3 4
Using the wildcard *.ttf protected against using non-fonts in PIL as ImageFont objects. listdir() doesn’t support wildcards, so glob.glob() was required.
Take that, spam!