Skip to main content

Django recaptcha form field/widget app.

Project description

Django reCAPTCHA

Django reCAPTCHA form field/widget integration app.

django-recaptcha uses a modified version of the Python reCAPTCHA client which is included in the package as client.py.

Installation

  1. Install or add django-recaptcha to your Python path.

  2. Add captcha to your INSTALLED_APPS setting.

  3. Add a RECAPTCHA_PUBLIC_KEY setting to the project’s settings.py file. This is your public API key as provided by reCAPTCHA, i.e.:

    RECAPTCHA_PUBLIC_KEY = '76wtgdfsjhsydt7r5FFGFhgsdfytd656sad75fgh'

    This can be seperately specified at runtime by passing a public_key parameter when constructing the ReCaptchaField, see field usage below.

  4. Add a RECAPTCHA_PRIVATE_KEY setting to the project’s settings.py file. This is your private API key as provided by reCAPTCHA, i.e.:

    RECAPTCHA_PRIVATE_KEY = '98dfg6df7g56df6gdfgdfg65JHJH656565GFGFGs'

    This can be seperately specified at runtime by passing a private_key parameter when constructing the ReCaptchaField, see field usage below.

  5. Optionally add a RECAPTCHA_USE_SSL setting to the project’s settings.py file. This causes reCAPTCHA validation submits to be made over SSL, i.e.:

    RECAPTCHA_USE_SSL = True

    If you don’t add this setting the default behaviour is to NOT use SSL. This can be seperately specified at runtime by passing a use_ssl parameter when constructing the ReCaptchaField, see field usage below.

Usage

Field

The quickest way to add reCAPTHCA to a form is to use the included ReCaptchaField field type. A ReCaptcha widget will be rendered with the field validating itself without any further action required from you. For example:

from django import forms
from captcha.fields import ReCaptchaField

class FormWithCaptcha(forms.Form):
    captcha = ReCaptchaField()

To allow for runtime specification of keys and SSL usage you can optionally pass private_key, public_key or use_ssl parameters to the constructor, i.e.:

captcha = ReCaptchaField(
    public_key='76wtgdfsjhsydt7r5FFGFhgsdfytd656sad75fgh',
    private_key='98dfg6df7g56df6gdfgdfg65JHJH656565GFGFGs',
    use_ssl=True
)

If specified these parameters will be used instead of your reCAPCTHA project settings.

The reCAPTCHA widget supports several Javascript options variables customizing the behaviour of the widget, such as theme and lang. You can forward these options to the widget by passing an attr parameter containing a dictionary of options to ReCaptchaField, i.e.:

captcha = ReCaptchaField(attrs={'theme' : 'clean'})

The captcha client takes the key/value pairs and writes out the RecaptchaOptions value in JavaScript.

Unit Testing

django-recaptcha introduces an environmental variable RECAPTCHA_TESTING which helps facilitate tests. The environmental variable should be set to “True”, and cleared, using the setUp() and tearDown() methods in your test classes.

Setting RECAPTCHA_TESTING to True causes django-recaptcha to accept “PASSED” as the recaptcha_response_field value.

Example::

import os
os.environ['RECAPTCHA_TESTING'] = 'True'

form_params = {'recaptcha_response_field': 'PASSED'}
form = RegistrationForm(form_params) # assuming only one ReCaptchaField
form.is_valid() # True

os.environ['RECAPTCHA_TESTING'] = 'False'
form.is_valid() # False

Passing any other values will cause django-recaptcha to continue normal processing and return a form error.

Check tests.py for a full example.

AJAX

To make Recapcha work in ajax-loaded forms:

  1. import recaptcha_ajax.js on your page (not in the loaded template):

    <script type=”text/javascript” src=”http://www.google.com/recaptcha/api/js/recaptcha_ajax.js”></script>

  2. settings.py

    CAPTCHA_AJAX = True

Credits

Inspired Marco Fucci’s blogpost titled Integrating reCAPTCHA with Django

client.py taken from recaptcha-client licenced MIT/X11 by Mike Crawford.

reCAPTCHA copyright 2012 Google.

Authors

Praekelt Foundation

  • Shaun Sephton

  • Peter Pistorius

  • Hedley Roos

bTaylor Design

Other

Changelog

0.0.8 (2014-02-13)

  1. Bugfix: remove reference to options.html.

0.0.7 (2014-02-12)

  1. Make it possible to load the widget via ajax.

0.0.6 (2013-01-31)

  1. Added an extra parameter lang to bypass Google’s language bug. See http://code.google.com/p/recaptcha/issues/detail?id=133#c3

  2. widget.html no longer includes options.html. Options are added directly to widget.html

0.0.5 (2013-01-17)

  1. Removed django-registration dependency

  2. Changed testing mechanism to environmental variable RECAPTCHA_TESTING

0.0.4

  1. Handle missing REMOTE_ADDR request meta key. Thanks Joe Jasinski.

  2. Added checks for settings.DEBUG to facilitate tests. Thanks Victor Neo.

  3. Fix for correct iframe URL in case of no javascript. Thanks gerdemb.

0.0.3 (2011-09-20)

  1. Don’t force registration version thanks kshileev.

  2. Render widget using template, thanks denz.

0.0.2 (2011-08-10)

  1. Use remote IP when validating.

  2. Added SSL support, thanks Brooks Travis.

  3. Added support for Javascript reCAPTCHA widget options, thanks Brandon Taylor.

  4. Allow for key and ssl specification at runtime, thanks Evgeny Fadeev.

0.0.1 (2010-06-17)

  1. Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-recaptcha-0.0.8.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

django_recaptcha-0.0.8-py2.7.egg (14.3 kB view details)

Uploaded Source

File details

Details for the file django-recaptcha-0.0.8.tar.gz.

File metadata

File hashes

Hashes for django-recaptcha-0.0.8.tar.gz
Algorithm Hash digest
SHA256 e773509c3edcf0f3fc8dedd8e2dae6cc979d3ae16b9fa756417b7f67895c6b95
MD5 e7a3c4627e381032d9a2de67d40a0db9
BLAKE2b-256 9349bf50c8a6d6934c9c681fac1e07c2995607c94e5bd3acc5f72136ec96a16c

See more details on using hashes here.

Provenance

File details

Details for the file django_recaptcha-0.0.8-py2.7.egg.

File metadata

File hashes

Hashes for django_recaptcha-0.0.8-py2.7.egg
Algorithm Hash digest
SHA256 e71fff4d85286a48faed2ac131a518334141841f599a66922bd1aeecc08ec886
MD5 755f381caf64ef851dcba5e2b2f59d04
BLAKE2b-256 76d83b1ebb2c7941df43eb0b1b6050cb128f80ca64480970a9df3e1ef0e4c562

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page