Add Cloudflare Turnstile validator widget to the forms of your django project.
This project refers to github project django-hcaptcha (author: AndrejZbin)
Configuration
Add “turnstile” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [
...
'turnstile',
]
For development purposes no further configuration is required. By default, django-Turnstile will use dummy keys.
For production, you’ll need to obtain your Turnstile site key and secret key and add them to you settings:
TURNSTILE_SITEKEY = '<your sitekey>' TURNSTILE_SECRET = '<your secret key>'
You can also configure your Turnstile widget globally (see all options):
TURNSTILE_DEFAULT_CONFIG = {
'onload': 'name_of_js_function',
'render': 'explicit',
'theme': 'dark', # do not use data- prefix
'size': 'compact', # do not use data- prefix
...
}
If you need to, you can also override default turnstile endpoints:
TURNSTILE_JS_API_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js' TURNSTILE_VERIFY_URL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'
Use proxies:
TURNSTILE_PROXIES = {
'http': 'http://127.0.0.1:8000',
}
Change default verification timeout:
TURNSTILE_TIMEOUT = 5
Usage
Simply add TurnstileField to your forms:
from turnstile.fields import TurnstileField
class Forms(forms.Form):
....
turnstile = TurnstileField()
....
In your template, if you need to, you can then use {{ form.turnstile }} to access the field.
You can override default config by passing additional arguments:
class Forms(forms.Form):
....
turnstile = TurnstileField(theme='dark', size='compact')
....
How it Works
When a form is submitted by a user, Turnstile’s JavaScript will send one POST parameter to your backend: cf-turnstile-response. It will be received by your app and will be used to complete the turnstile form field in your backend code.
When your app receives these two values, the following will happen:
Your backend will send these values to the Cloudflare Turnstile servers
Their servers will indicate whether the values in the fields are correct
If so, your turnstile form field will validate correctly
Unit Tests
You will need to disable the Turnstile field in your unit tests, since your tests obviously cannot complete the Turnstile successfully. One way to do so might be something like:
from unittest.mock import MagicMock, patch
from django.test import TestCase
@patch("turnstile.fields.TurnstileField.validate", return_value=True)
class ContactTest(TestCase):
test_msg = {
"name": "pandora",
"message": "xyz",
"turnstile": "xxx", # Any truthy value is fine
}
def test_something(self, mock: MagicMock) -> None:
response = self.client.post("/contact/", self.test_msg)
self.assertEqual(response.status_code, HTTP_302_FOUND)
Remote IP
In order to improve bot detection, Turnstile accepts an optional parameter remoteip with the the visitor’s IP address. During server-side validation, it’s sent to Cloudflare and it’s used as bonus/malus during bot index calculation. Before using it, make sure to verify whether your country requires a notice or disclaimer, since you may be collecting user information.
remote_ip should be passed from view to form to Turnstile field, you can find an example below. Note that this is an example, in order to get real client IP REMOTE_ADDR may not be enough.
# views.py
def my_view(request):
remote_ip = request.META.get('REMOTE_ADDR')
form = MyForm(request.POST or None, remote_ip=remote_ip)
# forms.py
class MyForm(forms.Form):
turnstile = TurnstileField()
def __init__(self, *args, remote_ip=None, **kwargs):
super().__init__(*args, **kwargs)
if remote_ip:
self.fields['turnstile'].remote_ip = remote_ip
Release files for django-turnstile 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_turnstile-0.1.3.tar.gz | 5.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_turnstile-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.7 kB
Release files / django_turnstile-0.1.3.tar.gz
| Download URL | django_turnstile-0.1.3.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5079eb937938ba9a475c1a80c146fe1e71de025f25f3e572a911ae926bd5120d
|
|
BLAKE2b-256 checksum How to use checksums |
cbe5795fc00bae0bdbf8fd72d3baa3b700174481ddd7096ba06ee4a7c1fed0af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Jan 18, 2026.
Transparency logRelease files / django_turnstile-0.1.3-py3-none-any.whl
| Download URL | django_turnstile-0.1.3-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f7c82b020d5bd0fcaccb2ffac089809afb220671595c55a46a987283d7d5e61b
|
|
BLAKE2b-256 checksum How to use checksums |
686f8d4d5cd527d0cd42a894d51ce750dc3bae50a6cbed8294cfc870d119502a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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 Jan 18, 2026.
Transparency log