Skip to main content

Invisible, layered form protection for Django. Honeypot fields, signed timestamps, and a pluggable check pipeline.

Project description

django-formguard

Invisible form protection for Django. A pluggable check pipeline that catches bots without any user interaction.

How It Works

FormGuard runs a pipeline of checks against each form submission:

  1. Field trap - a hidden honeypot field that should always be empty. Bots that fill all visible fields get caught.
  2. Token - a signed timestamp records when the form was loaded. Submissions faster than MIN_SECONDS or with tampered/expired tokens are flagged.
  3. JS challenge - a random nonce is embedded in the form. JavaScript computes a value from it that bots without JS execution can't produce.

All checks are invisible to real users. Bots receive a fake success response indistinguishable from a real one.

The pipeline is extensible - each check is a class that owns its form fields, media, and settings. See Custom Checks.

Installation

pip install django-formguard

Add 'formguard' to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'formguard',
]

Quick Start

1. Define your form

# forms.py
from django import forms
from formguard.forms import GuardedFormMixin

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

2. Include {{ form.media }} in your template

FormGuard's CSS and JS are declared as form media. Include {{ form.media }} in your template's <head> so the honeypot field is hidden and the JS challenge runs.

<!-- contact.html -->
<head>
    {{ form.media }}
</head>
<body>
    <form method="post">
        {% csrf_token %}
        {{ form }}
        <button type="submit">Send</button>
    </form>
</body>

If your template renders fields manually instead of using {{ form }}, use {{ form.guard_fields }} to render all guard fields at once:

<form method="post">
    {% csrf_token %}
    {{ form.guard_fields }}
    <label>Name</label>
    {{ form.name }}
    <button type="submit">Send</button>
</form>

3. Check submissions in your view

Class-based views - use GuardedFormViewMixin:

from django.views.generic import FormView
from formguard.views import GuardedFormViewMixin

class ContactView(GuardedFormViewMixin, FormView):
    form_class = ContactForm
    template_name = 'contact.html'
    success_url = '/thanks/'

    def form_valid(self, form):
        if self.is_bot(form):
            return self.bot_response()
        send_email(form.cleaned_data)
        return super().form_valid(form)

Function-based views - use the @guard_form decorator:

from formguard.decorators import guard_form

@guard_form(form_class=ContactForm, success_url='/thanks/')
def contact_view(request, form=None):
    # only reached if the form is clean (bots were already redirected)
    send_email(form.cleaned_data)
    return redirect('/thanks/')

Settings

All settings are optional. Defaults work out of the box.

Setting Default Description
FORMGUARD_SUCCESS_MESSAGE None Fake success message shown to bots (via Django messages)
FORMGUARD_CHECKS (see below) Ordered list of check class paths
FORMGUARD_FIELD_TRAP_FIELD_NAME 'website' Honeypot field name (label derived via .title())
FORMGUARD_TOKEN_MIN_SECONDS 3 Minimum seconds before a submission is accepted
FORMGUARD_TOKEN_MAX_SECONDS 3600 Maximum token age before expiry

Default checks:

FORMGUARD_CHECKS = [
    'formguard.checks.FieldTrapCheck',
    'formguard.checks.TokenCheck',
    'formguard.checks.JsChallengeCheck',
]

Further Reading

CSP Compatibility

FormGuard uses external static files for CSS and JS (no inline styles or scripts), so it works with Content Security Policy out of the box when 'self' is in your script-src and style-src.

For strict nonce-based CSP policies, use django-csp-helpers to add nonces to the {{ form.media }} output.

License

This software is released under the MIT license.

Copyright (c) 2026 Luke Rogers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

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_formguard-0.2.1.tar.gz (47.1 kB view details)

Uploaded Source

Built Distribution

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

django_formguard-0.2.1-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

Details for the file django_formguard-0.2.1.tar.gz.

File metadata

  • Download URL: django_formguard-0.2.1.tar.gz
  • Upload date:
  • Size: 47.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_formguard-0.2.1.tar.gz
Algorithm Hash digest
SHA256 29a0eaac5984c80f71b429fc9a8b27199cda7e8a0a328e1a96cce3446a518055
MD5 08059d33e982f8bf6b45dba10ccb7d52
BLAKE2b-256 171882f5acf7dd5e6d6b66c2d71e67ecee5d80db1a4e3118e681630f693c4678

See more details on using hashes here.

File details

Details for the file django_formguard-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: django_formguard-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 22.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for django_formguard-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc511501e76acd4da894241115da45ec21ea3eeb9eeee624d3d19a8eb323ed6c
MD5 635908eaf8ab53a8f0f11430ab8f59ef
BLAKE2b-256 4c3254e7a0cfac1d8de93b593bdac5e773aaa9d12381466ef65cf1debb798c10

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