Skip to main content

Django reCAPTCHA

Django reCAPTCHA form field/widget integration app.

PyPI latest version PyPI monthly downloads CI status Coverage Status

[!NOTE] django-recaptcha supports Google reCAPTCHA V2 - Checkbox (Default), Google reCAPTCHA V2 - Invisible and Google reCAPTCHA V3. Please look at the widgets section for more information.

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

Requirements

Tested with:

This package only supports modern, “evergreen” desktop and mobile browsers. For IE11 support, make sure to add a polyfill for Element.closest.

Installation

  1. Sign up for reCAPTCHA.

  2. Install with pip install django-recaptcha.

  3. Add 'django_recaptcha' to your INSTALLED_APPS setting.

INSTALLED_APPS = [
    ...,
    'django_recaptcha',
    ...
]
  1. Add the Google reCAPTCHA keys generated in step 1 to your Django production settings with RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY. Note that omitting these settings will default to a set of test keys refer to Local Development and Functional Testing for more information.

For example:

RECAPTCHA_PUBLIC_KEY = 'MyRecaptchaKey123'
RECAPTCHA_PRIVATE_KEY = 'MyRecaptchaPrivateKey456'

These can also be specified per field by passing the public_key or private_key parameters to ReCaptchaField - see field usage below.

  1. (OPTIONAL) If you require a proxy, add a RECAPTCHA_PROXY setting (dictionary of proxies), for example:
RECAPTCHA_PROXY = {'http': 'http://127.0.0.1:8000', 'https': 'https://127.0.0.1:8000'}
  1. (OPTIONAL) In the event www.google.com is not accessible the RECAPTCHA_DOMAIN setting can be changed to www.recaptcha.net as per the reCAPTCHA FAQ:
RECAPTCHA_DOMAIN = 'www.recaptcha.net'

This will change the Google JavaScript api domain as well as the client side field verification domain.

Usage

Fields

The quickest way to add reCAPTCHA to a form is to use the included ReCaptchaField field class. A ReCaptchaV2Checkbox will be rendered by default. For example:

from django import forms
from django_recaptcha.fields import ReCaptchaField

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

Be sure to include the captcha field in your forms. There are many ways to add fields to forms in Django. We recommend you refer to the form rendering options and rendering fields manually sections of the official Django documentation for forms.

To allow for runtime specification of keys you can optionally pass the private_key or public_key parameters to the constructor. For example:

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

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

Widgets

There are three widgets that can be used with the ReCaptchaField class:

To make use of widgets other than the default Google reCAPTCHA V2 - Checkbox widget, simply replace the ReCaptchaField widget. For example:

from django import forms
from django_recaptcha.fields import ReCaptchaField
from django_recaptcha.widgets import ReCaptchaV2Invisible

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

The reCAPTCHA widget supports several data attributes that customize the behaviour of the widget, such as data-theme, data-size, etc. You can forward these options to the widget by passing an attrs parameter to the widget, containing a dictionary of options. For example:

captcha = fields.ReCaptchaField(
    widget=widgets.ReCaptchaV2Checkbox(
        attrs={
            'data-theme': 'dark',
            'data-size': 'compact',
        }
    )
)
# The ReCaptchaV2Invisible widget
# ignores the "data-size" attribute in favor of 'data-size="invisible"'

The reCAPTCHA api supports several parameters. To customise the parameters that get sent along pass an api_params parameter to the widget, containing a dictionary of options. For example:

captcha = fields.ReCaptchaField(
    widget=widgets.ReCaptchaV2Checkbox(
        api_params={'hl': 'cl', 'onload': 'onLoadFunc'}
    )
)
# The dictionary is urlencoded and appended to the reCAPTCHA api url.

By default, the widgets provided only supports a single form with a single widget on each page.

The language can be set with the 'hl' parameter, look at language codes for the language code options. Note that translations need to be added to this package for the errors to be shown correctly. Currently the package has error translations for the following language codes: es, fr, nl, pl, pt_BR, ru, zh_CN, zh_TW

However, the JavaScript used by the widgets can easily be overridden in the templates.

The templates are located in:

  • django_recaptcha/includes/js_v2_checkbox.html for overriding the reCAPTCHA V2 - Checkbox template
  • django_recaptcha/includes/js_v2_invisible.html for overriding the reCAPTCHA V2 - Invisible template
  • django_recaptcha/includes/js_v3.html for overriding the reCAPTCHA V3 template

For more information about overriding templates look at Django's template override

reCAPTCHA V3 Score

As of version 3, reCAPTCHA also returns a score value. This can be used to determine the likelihood of the page interaction being a bot. See the Google documentation for more details.

To set a project wide score limit use the RECAPTCHA_REQUIRED_SCORE setting.

For example:

RECAPTCHA_REQUIRED_SCORE = 0.85

For per field, runtime, specification the score can also be passed to the widget:

captcha = fields.ReCaptchaField(
    widget=ReCaptchaV3(required_score=0.85)
)

In the event the score does not meet the requirements, the field validation will fail as expected and an error message will be logged.

reCAPTCHA V3 Action

Google's reCAPTCHA V3 API supports passing an action value. Actions allow you to tie reCAPTCHA validations to a specific form on your site for analytical purposes, enabling you to perform risk analysis per form. This will allow you to make informed decisions about adjusting the score threshold for certain forms because abusive behavior can vary depending on the nature of the form.

To set the action value, pass an action argument when instantiating the ReCaptcha widget. Be careful to only use alphanumeric characters, slashes and underscores as stated in the reCAPTCHA documentation.

captcha = fields.ReCaptchaField(
    widget=widgets.ReCaptchaV3(
        action='signup'
    )
)

Setting an action is entirely optional. If you don't specify an action, no action will be passed to the reCAPTCHA V3 API.

Local Development and Functional Testing

If RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY are not set, django-recaptcha will use Google's test keys instead. These cannot be used in production since they always validate to true and a warning will be shown on the reCAPTCHA. Google's test keys only work for reCAPTCHA version 2.

To bypass the security check that prevents the test keys from being used unknowingly add SILENCED_SYSTEM_CHECKS = [..., 'django_recaptcha.recaptcha_test_key_error', ...] to your settings, here is an example:

SILENCED_SYSTEM_CHECKS = ['django_recaptcha.recaptcha_test_key_error']

If you want to mock the call to Google's servers altogether, have a look at test_fields.py:

from unittest.mock import patch
from django.test import TestCase
from django_recaptcha.client import RecaptchaResponse

class TestFields(TestCase):
    @patch("django_recaptcha.fields.client.submit")
    def test_client_success_response(self, mocked_submit):
        mocked_submit.return_value = RecaptchaResponse(is_valid=True)
        ...

Credits

Originally developed by Praekelt Consulting.

Inspired Marco Fucci's blogpost titled Integrating reCAPTCHA with Django.

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

reCAPTCHA copyright 2012 Google.

Release files for django-recaptcha 4.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-recaptcha 4.1.0
File Size Uploaded
django_recaptcha-4.1.0.tar.gz 28.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-recaptcha 4.1.0
File Interpreter ABI Platform
django_recaptcha-4.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 68.1 kB

Release files / django_recaptcha-4.1.0.tar.gz

Download URL django_recaptcha-4.1.0.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
73097b958733f65b729d4e4630c51fc2bf147aca6f026017d18898dfc25cb4c5
BLAKE2b-256 checksum
How to use checksums
bda2480cd2df8031c30afe0d67b0a5e6299ced20f2e65135a3c160c4031d7052
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 28, 2025.

Transparency log

Release files / django_recaptcha-4.1.0-py3-none-any.whl

Download URL django_recaptcha-4.1.0-py3-none-any.whl
Size 39.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
463aa65967e973de466b28ce8e8b6abb2faffb1ce0c7faa1bf0e5b041e0497cb
BLAKE2b-256 checksum
How to use checksums
8be70765f98e4953e26573512284995648c439b0eb252877b655683d28c90be2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 28, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

4.1.0 This release

2 release files

4.0.0

2 release files

3.0.0

2 release files

2.0.6

1 release file

2.0.5

3 release files

2.0.4

3 release files

2.0.2

2 release files

2.0.1

3 release files

2.0.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

1 release file

1.0.3

2 release files

1.0.2

1 release file

1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

3 release files

0.0.3

3 release files

0.0.2

3 release files

0.0.1

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page