django-cf-turnstile is a Django app for integrating Cloudflare Turnstile captchas into django forms.
Note that django-cf-turnstile only supports implicit rendering of widgets.
- Python: 3.10+
- Django: 4.2+
Install the package with:
pip install django-cf-turnstile
Add "django_cf_turnstile" to your INSTALLED_APPS setting.
INSTALLED_APPS = [
...,
"django_cf_turnstile",
]
Add your Turnstile site key and secret to your django settings.
# django-cf-turnstile
# Dummy keys used below, replace with your actual keys
CF_TURNSTILE_SITE_KEY = "1x00000000000000000000AA"
CF_TURNSTILE_SECRET_KEY = "1x0000000000000000000000000000000AA"
Note that if you don't provide a site key and secret key the test site keys which always pass validation will be used.
For more information on test keys see https://developers.cloudflare.com/turnstile/troubleshooting/testing/.
In your Django forms import and use TurnstileCaptchaField
from django import forms
from django_cf_turnstile.fields import TurnstileCaptchaField
class CaptchaSignupForm(forms.Form):
...
captcha = TurnstileCaptchaField()
Keys may be overridden on individual forms by passing in site_key
and secret_key
.
class CaptchaSignupForm(forms.Form):
captcha = TurnstileCaptchaField(
site_key="3x00000000000000000000FF",
secret_key="1x0000000000000000000000000000000AA"
)
By default, the label is set to an empty string.
This can be overridden by setting label
.
class CaptchaSignupForm(forms.Form):
captcha = TurnstileCaptchaField(label="Captcha")
Inspired by django-recaptcha