Snippet List
My previous snippet with captcha wasn't very portable but Marco Fucci figured out the thing that I couldn't - value_from_datadict function. So all credits go to him and his snippet, I adapted it to my needs, maybe you like my version better - it doesn't need any captcha libraries and let's you modify the widget's html easily. Also I added an option to pass remoteip to google api's verify method.
How to use it:
In your settings.py add:
RECAPTCHA_PUBKEY = 'your recaptcha public key'
RECAPTCHA_PRIVKEY = 'your recaptcha private key'
After that just import and use ReCaptchaField in your form as you would any other field. That's it.
*** Important *** If you want to have peace of mind in case google decided that the remoteip parametr is mandatory then:
Derive every form that has the captcha field from ReCaptchaForm and when you create the form object after receiving POST/GET, pass a remoteip parameter like that:
form = YourCaptchaForm(data=request.POST, remoteip=request.META['REMOTE_ADDR'])
It is not so portable and easy as I wanted it to be because of how django forms work - they don't play well with recaptcha.
To get it to work:
* Add two variables to your app settings, **RECAPTCHA_PUBKEY** and **RECAPTCHA_PRIVKEY**
* Derive forms you want to have a captcha from the provided `ReCaptchaForm` class (how to get it working with ModelForm? any ideas?)
* * If you override the form's clean method make sure you firstly call the `ReCaptchaForm`'s clean method *
* In your view, upon receiving the form data initialize the objects like this `form = YouFormClassDerivedFromReCaptchaForm(remoteip=request.META['REMOTE_ADDR'], data=request.POST)` (or request.GET of course) - this is because reCaptcha needs the user's remote ip.
Integrating [reCAPTCHA](http://recaptcha.net/) with Django.
**Warning**: although *remoteip* is used as optional parameter in this snippet, it is likely to become mandatory in the future. Please see [the comment by Jim Garrison](http://www.marcofucci.com/tumblelog/26/jul/2009/integrating-recaptcha-with-django/#comment-262) for more detail.
Generic version of [this snippet](http://www.djangosnippets.org/snippets/433/).
1. Register on [reCAPTCHA](http://recaptcha.net/) to get your public/private key pair
2. Add your keys in settings.py
3. Add [recaptcha-client](http://pypi.python.org/pypi/recaptcha-client) to your project
4. Put ReCaptchaField and ReCaptcha widget somewhere (I've used a generic app `marcofucci_utils`)
5. Configure your form
More information on my website [marcofucci.com](http://www.marcofucci.com/tumblelog/26/jul/2009/integrating-recaptcha-with-django/).
There already is a [snippet on here](http://www.djangosnippets.org/snippets/433/) that shows how to use reCAPTCHA with Django, but being the lazya-- that I am, I wanted the form to:
* display the captcha when as_table is used
* handle captcha validation for me
So RecaptchaForm does the trick. Just subclass from it and voila, your form will include reCAPTCHA validation.
Other than that, you need to
* be on Django trunk version (or use clean_data instead of cleaned_data for 0.96)
* set RECAPTCHA_PRIVATE_KEY and RECAPTCHA_PUBLIC_KEY in your settings.py
* make sure that the [captcha module](http://pypi.python.org/pypi/recaptcha-client) is available somewhere (i.e. that "import captcha" works)
Working off b23's [recaptcha support](http://www.djangosnippets.org/snippets/433/), I have hacked a way to add recaptcha support using existing comments. I am sure there is a better way, and ultimately I will suggest a patch to add captcha support as an option, but for now I hope this helps. For a more detailed rundown (as well as a working sample), check out my [blog entry](http://nikolajbaer.us/blog/recaptcha-django-free-comments/).
Recaptcha is a free service that helps you protect your forms against spam bots by displaying a picture showing 2 words or playing an audio file telling 8 digits for the visually handicapped.
After registration on http://recaptcha.net a private and a public key are
generated. Put the keys in settings.py.
Find client code for recaptcha at http://pypi.python.org/pypi/recaptcha-client. Put the file captcha.py into application root.
7 snippets posted so far.