Code Monkey home page Code Monkey logo

django-turnstile's Introduction

Django Turnstile

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)

django-turnstile's People

Contributors

zmh-program avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

django-turnstile's Issues

Multiple turnstile per site

Hi, I came across your package for Django. Saved me a few hours on implementing CF Turnstile on my new site. Thank you, I reviewed every file in your repo, and it is well-developed! Thank you for sharing your package!
I have a question regarding the Turnsite site keys. The document for this package shows how to add your Turnsite site key/secret to the settings.py. I had no problem implementing the initial Turnsite. I normally, create a Turnstile for each individual form, to keep track of the individual analytics in CF. I tried to bring it down to one Turnstile and just use Matomo Analytics but, I definitely need two different Turnstile at the bare minimum. I need a managed Turnstile and an invisible Turnstile for one particular form. I'm not sure how to implement two different site/secret keys and point them to the individual Turnstile? Is that possible with your package?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.