Skip to main content

Fully server-side captcha for Django — math, image, and word challenges with zero external API calls.

Project description

django-server-captcha

Fully server-side captcha for Django — math, image, and word challenges with zero external API calls.

No third-party services, no API keys, no external requests. Everything runs on your own server.

Features

  • Math Captcha — arithmetic expression the user must solve
  • Image Captcha — 3x3 SVG shape grid; select images matching a color+shape criteria
  • Word Captcha — reversed word the user must type correctly
  • Refresh buttons — each captcha can be regenerated independently via AJAX
  • Session-based — answers stored in Django's session framework (no database required)
  • Fully server-side — no Google/reCAPTCHA keys, no external HTTP calls

Requirements

  • Python 3.9+
  • Django 4.2+

Installation

pip install django-server-captcha

Setup

  1. Add 'captcha' to your INSTALLED_APPS:
# settings.py
INSTALLED_APPS = [
    ...
    'captcha',
]
  1. Make sure django.contrib.sessions is in INSTALLED_APPS and SessionMiddleware is in MIDDLEWARE (both are enabled by default).

  2. Run a migrate (for the sessions table if you haven't already):

python manage.py migrate

Usage

In a view

from captcha.views import CaptchaFormView

urlpatterns = [
    path('verify/', CaptchaFormView.as_view(), name='captcha_form'),
]

In your own view

from captcha.forms import MathCaptchaForm, ImageCaptchaForm, WordCaptchaForm

def my_view(request):
    math_form = MathCaptchaForm(request.POST or None, session=request.session)
    image_form = ImageCaptchaForm(request.POST or None, session=request.session)
    word_form = WordCaptchaForm(request.POST or None, session=request.session)

    if request.method == 'POST':
        if math_form.is_valid() and image_form.is_valid() and word_form.is_valid():
            # All captchas passed
            ...

In a template

<form method="post">
    {% csrf_token %}

    {{ math_form.answer }}
    {{ math_form.answer.errors }}

    {{ image_form.selected }}
    {{ image_form.selected.errors }}

    {{ word_form.word }}
    {{ word_form.word.errors }}

    <button type="submit">Verify</button>
</form>

Refresh endpoint

The package includes a JSON endpoint for refreshing captchas via AJAX:

# urls.py
path('captcha/refresh/', 'captcha.views.captcha_refresh', name='captcha_refresh'),
fetch("/captcha/refresh/", {
    method: "POST",
    headers: {"X-CSRFToken": csrfToken},
    body: new URLSearchParams({type: "math"})
})
.then(r => r.json())
.then(data => {
    document.getElementById("math-display").textContent = data.display;
});

Valid type values: "math", "image", "word".

License

MIT

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

django_server_captcha-1.0.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

django_server_captcha-1.0.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file django_server_captcha-1.0.0.tar.gz.

File metadata

  • Download URL: django_server_captcha-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for django_server_captcha-1.0.0.tar.gz
Algorithm Hash digest
SHA256 325a655f487de8eb7efc43cba5505014c8da19fb275dc71706598a8d0022e422
MD5 455c34df4f7eb010713dcb0939fd5542
BLAKE2b-256 5a907c1d434d93b20737fd86688f6060ee5ade4dcda9db3069e424f85cad5c85

See more details on using hashes here.

File details

Details for the file django_server_captcha-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_server_captcha-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2396ab8a33e67fde5c07573e9c53e251ae014e34c1c8f00de1ce75e745099cef
MD5 d4efa93e7089d86ce1c80f020d16eadc
BLAKE2b-256 0769184a81f44bab9716ff601e6eaeb03d22b964c27a87d4e5a21f2cc882a7b5

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