Integrations

Django

Django integration guide

Integration guide for Django applications.

Full Example

A complete working example is available on GitHub: Captchacat-Integrations/Python-Django

Configuration#

Add Captchacat settings to your Django settings:

settings.py
CAPTCHACAT_API_KEY = ""   # Your API key
CAPTCHACAT_SITE_KEY = ""  # Your site key

Validator#

Create a validator class:

validators.py
import requests
from django.conf import settings
 
class CaptchacatValidator:
    @staticmethod
    def validate(token: str) -> bool:
        if not token:
            return False
 
        response = requests.post(
            "https://challenge.captchacat.com/validate_token",
            json={
                "api_key": settings.CAPTCHACAT_API_KEY,
                "token": token
            },
            timeout=10
        )
        return response.ok

Template#

Add the widget to your form template:

templates/index.html
<!DOCTYPE html>
<html>
<head>
  <title>Login Form</title>
  <script src="https://challenge.captchacat.com/ray/widget.js"></script>
</head>
<body>
  <form action="{% url 'submit' %}" method="POST">
    {% csrf_token %}
 
    <input type="text" name="username" placeholder="Username" required />
    <input type="password" name="password" placeholder="Password" required />
 
    <div class="captcha-widget" data-sitekey="{{ site_key }}"></div>
 
    <button type="submit">Sign In</button>
  </form>
</body>
</html>

View#

views.py
from django.shortcuts import render
from django.conf import settings
from .validators import CaptchacatValidator
 
def index(request):
    return render(request, "index.html", {
        "site_key": settings.CAPTCHACAT_SITE_KEY,
    })
 
def submit(request):
    if request.method != "POST":
        return HttpResponse("Method not allowed", status=405)
 
    token = request.POST.get("captchacat-token", "")
 
    if not CaptchacatValidator.validate(token):
        return render(request, "index.html", {
            "site_key": settings.CAPTCHACAT_SITE_KEY,
            "error": "CAPTCHA verification failed",
        })
 
    # Token valid - process the form
    username = request.POST.get("username")
    # ... handle form data
 
    return render(request, "result.html", {
        "message": "Login successful!",
    })

Built with precision. Designed for developers.