Skip to main content

Django integration for Myra EU Captcha

Project description

Myra EU Captcha - Django Integration

Django integration for the Myra EU Captcha service. Provides form fields, widgets, and an admin interface for managing captcha configurations.

Installation

pip install myra-eucaptcha-django

Or with uv:

uv add myra-eucaptcha-django

Quick Start

1. Add to INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    ...
    'myra_eucaptcha_django',
]

2. Run Migrations

python manage.py migrate

3. Configure via Admin

  1. Go to Django Admin → Captcha Configurations
  2. Add a new configuration with your sitekey and secret
  3. Mark it as Default if you want it used automatically

4. Add to Your Form

from django import forms
from myra_eucaptcha_django import CaptchaField, CaptchaFormMixin

class ContactForm(CaptchaFormMixin, forms.Form):
    name = forms.CharField()
    email = forms.EmailField()
    message = forms.CharField(widget=forms.Textarea)

    # Uses the default configuration
    captcha = CaptchaField()

5. Use in Your View

from django.shortcuts import render, redirect
from .forms import ContactForm

def contact_view(request):
    if request.method == "POST":
        # Pass request= to enable IP validation
        form = ContactForm(request.POST, request=request)
        if form.is_valid():
            # Process the form
            return redirect("success")
    else:
        form = ContactForm()

    return render(request, "contact.html", {"form": form})

6. Render in Template

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Send</button>
</form>

The captcha widget automatically includes the required JavaScript.

Multiple Configurations

You can define multiple captcha configurations for different forms or purposes.

In Admin

Create multiple configurations with unique names:

  • default (marked as default)
  • contact-form
  • registration

In Forms

Reference configurations by name:

class ContactForm(CaptchaFormMixin, forms.Form):
    name = forms.CharField()
    captcha = CaptchaField("contact-form")

class RegistrationForm(CaptchaFormMixin, forms.Form):
    username = forms.CharField()
    captcha = CaptchaField("registration")

class CommentForm(CaptchaFormMixin, forms.Form):
    comment = forms.CharField()
    captcha = CaptchaField()  # Uses default configuration

Configuration Options

Each configuration in the admin supports:

Field Description Default
name Unique identifier for this config Required
description Optional notes about usage -
sitekey Public site key for client-side Required
secret Secret key for server-side verification Required
is_default Use when no config name specified False
is_active Allow this config to be used True

URLs (Advanced)

Field Description Default
verify_url Verification API endpoint https://api.eu-captcha.eu/v1/verify/
widget_url JavaScript widget URL https://cdn.eu-captcha.eu/verify.js

Timeouts (Advanced)

Field Description Default
connect_timeout Connection timeout (seconds) 3
read_timeout Read timeout (seconds) 10
write_timeout Write timeout (seconds) 10
pool_timeout Connection pool timeout (seconds) 3

Error Handling (Advanced)

Field Description Default
default_result_on_error Return success on network errors (fail-open) True
suppress_exceptions Suppress exceptions, return result instead True

Fallback Configuration via Settings

If you prefer not to use the database, configure via settings.py:

# settings.py
EUCAPTCHA_SITEKEY = "your-site-key"
EUCAPTCHA_SECRET = "your-secret-key"

# Optional
EUCAPTCHA_VERIFY_URL = "https://api.eu-captcha.eu/v1/verify/"
EUCAPTCHA_WIDGET_URL = "https://cdn.eu-captcha.eu/verify.js"
EUCAPTCHA_CONNECT_TIMEOUT = 3
EUCAPTCHA_READ_TIMEOUT = 10
EUCAPTCHA_WRITE_TIMEOUT = 10
EUCAPTCHA_POOL_TIMEOUT = 3
EUCAPTCHA_DEFAULT_RESULT_ON_ERROR = True
EUCAPTCHA_SUPPRESS_EXCEPTIONS = True

Database configurations take precedence over settings.

API Reference

CaptchaField

CaptchaField(config_name=None, **kwargs)

Form field that renders the captcha widget and validates the response.

  • config_name: Optional name of the configuration to use. If not specified, uses the default configuration.

CaptchaFormMixin

Mixin that automatically passes the request to captcha fields for IP validation.

class MyForm(CaptchaFormMixin, forms.Form):
    captcha = CaptchaField()

# In view:
form = MyForm(request.POST, request=request)

CaptchaWidget

The widget that renders the captcha challenge. Usually not used directly.

validate_captcha

Low-level validation function for custom use cases:

from myra_eucaptcha_django import validate_captcha

validate_captcha(
    token="captcha-response-token",
    remote_addr="client-ip",  # Optional
    config_name="my-config",  # Optional
)

Raises django.core.exceptions.ValidationError on failure.

License

Proprietary - Myra Security GmbH

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

myra_eucaptcha_django-0.1.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

myra_eucaptcha_django-0.1.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file myra_eucaptcha_django-0.1.0.tar.gz.

File metadata

  • Download URL: myra_eucaptcha_django-0.1.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.24

File hashes

Hashes for myra_eucaptcha_django-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b8a497c9b2d4ada7bc371389976582efb201dbf6379a089d3be3048f4f695516
MD5 960c12078c35527838f4ff4c14262088
BLAKE2b-256 b76422b5d55ecca3e60b9bb1b3987e5dafebafe8e1b38a093c15e36d63a0a773

See more details on using hashes here.

File details

Details for the file myra_eucaptcha_django-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for myra_eucaptcha_django-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8da006fa645a520607f0dff6e60c2166f2607d5318409bed4ee15451832118fc
MD5 d00d90708b2246f2cab2c4bee5edc02e
BLAKE2b-256 0ad996be2f2d2c4b290260f45d290feb894cf7c9734bb59f74dc3c5c5f2b6acc

See more details on using hashes here.

Supported by

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