Login

Include captchas from recaptcha.net

Author:
b23
Posted:
October 1, 2007
Language:
Python
Version:
.96
Tags:
captcha recaptcha
Score:
8 (after 8 ratings)

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
`
# settings.py

RECAPTCHA_PUB_KEY = "your public key"
RECAPTCHA_PRIVATE_KEY = "your private key"


# urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('',
        (r'^contact/$', 'myproject.myapp.views.contact'),
)


# views.py

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django import newforms as forms
from django.conf import settings
import captcha

class ContactForm(forms.Form):
    message = forms.CharField()

def contact(request):
    if request.method == 'POST':
        # Check the captcha
        check_captcha = captcha.submit(request.POST['recaptcha_challenge_field'], request.POST['recaptcha_response_field'], settings.RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR'])
        if check_captcha.is_valid is False:
            # Captcha is wrong show a error ...
            return HttpResponseRedirect('/url/error/')
        form = ContactForm(request.POST)
        if form.is_valid():
            # Do form processing here...
            return HttpResponseRedirect('/url/on_success/')
    else:
        form = ContactForm()
        html_captcha = captcha.displayhtml(settings.RECAPTCHA_PUB_KEY)
    return render_to_response('contact.html', {'form': form, 'html_captcha': html_captcha})


# contact.html

<form method="post" action="">
        {{ form.as_p }}
        {{ html_captcha }}
<input type="submit" />
</form>
`

More like this

  1. reCAPTCHA integration by marco.fucci 7 years, 1 month ago
  2. RecaptchaForm by oggy 8 years, 2 months ago
  3. Simple, stand-alone reCaptcha form module by SmileyChris 7 years, 1 month ago
  4. Recaptcha with Django Comments by nikolaj 8 years, 9 months ago
  5. Simple random file CAPTCHA by jeverling 4 years, 5 months ago

Comments

shacker (on August 7, 2009):
<p>Absolutely brilliant. Five-minute deploy, soup to nuts.</p>

#

shacker (on August 7, 2009):
<h1>Check the captcha</h1> <pre> check_captcha = captcha.submit( request.POST['recaptcha_challenge_field'], request.POST['recaptcha_response_field'], settings.RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR'] ) if check_captcha.is_valid is False: # Captcha is wrong show a error ... # return HttpResponseRedirect('/url/error/') pass_captcha = False html_captcha = captcha.displayhtml(settings.RECAPTCHA_PUB_KEY) else: pass_captcha = True if pass_captcha : if form.is_valid(): form.save(fail_silently=fail_silently) return HttpResponseRedirect(success_url) else: form = form_class(request=request,person=person) html_captcha = captcha.displayhtml(settings.RECAPTCHA_PUB_KEY) </pre>

#

shacker (on August 7, 2009):
<p>Oops - the goal of the comment above is to not use the /url/error redirect - instead this lets the user stay on the form but with the captcha redisplayed.</p>

#

TomL (on November 21, 2009):
<p>If you're using shacker's solution you'll probably want to change line 11 from:</p> <p>html_captcha = captcha.displayhtml(settings.RECAPTCHA_PUB_KEY)</p> <p>to:</p> <p>html_captcha = captcha.displayhtml(settings.RECAPTCHA_PUB_KEY, error=check_captcha.error_code )</p> <p>This will tell the user the error if the catcha is wrong/doesn't work.</p>

#

ivics (on July 16, 2010):
<p>Absolutely brilliant!!</p>

#

ereli (on February 22, 2012):
<p>one must also need to declare html_captch as global variable, or else it doesn't work.</p>

#

Please login first before commenting.