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:
- Field trap - a hidden honeypot field that should always be empty. Bots that fill all visible fields get caught.
- Token - a signed timestamp records when the form was loaded. Submissions
faster than
MIN_SECONDSor with tampered/expired tokens are flagged. - JS challenge - a random nonce is embedded in the form. JavaScript computes a value from it that bots without JS execution can't produce.
- Interaction proof - detects mouse, keyboard, or touch interaction with the form. Bots that POST directly without user input are flagged. Screen readers trigger this via keyboard and focus events.
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. Set up your template
Include {{ form.media }} in your <head> so the honeypot CSS and JS
checks load. If you render fields manually, use
{{ form.guard_fields }} to include all guard fields.
<head>
{{ form.media }}
</head>
<body>
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Send</button>
</form>
</body>
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. All four built-in checks are enabled by default.
# settings.py
# fake success message shown to bots (via Django messages)
FORMGUARD_SUCCESS_MESSAGE = 'Your message has been sent.'
# add a custom check alongside the defaults
from formguard.conf import DEFAULTS
FORMGUARD_CHECKS = DEFAULTS['CHECKS'] + [
'myapp.checks.TurnstileCheck',
]
# tune the built-in checks
FORMGUARD_FIELD_TRAP_FIELD_NAME = 'website' # default
FORMGUARD_TOKEN_MIN_SECONDS = 3 # default
FORMGUARD_TOKEN_MAX_SECONDS = 3600 # default
Further Reading
- Custom Checks - write your own checks (CAPTCHA, rate limiting, etc.)
- Advanced Usage - per-form checks, decorator options
- Testing - test helpers for guarded forms
- Signals - hook into bot detection events
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_formguard-0.3.0.tar.gz.
File metadata
- Download URL: django_formguard-0.3.0.tar.gz
- Upload date:
- Size: 38.7 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a814e33fe368cfdb01dd16c3e145b82b6b07ad080a9a6741a8ac73a70a41187
|
|
| MD5 |
2c6821f3907b524c0dea1f260f35b0ac
|
|
| BLAKE2b-256 |
37c11b836bafa84bff998755a064efea5aa4b8e436c25addc81b03a7fbf928b1
|
File details
Details for the file django_formguard-0.3.0-py3-none-any.whl.
File metadata
- Download URL: django_formguard-0.3.0-py3-none-any.whl
- Upload date:
- Size: 23.6 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77b76f6fca68da8ad1bf8fd4d16dfdf500e5a0c76e3db4354c58ec79065e65d5
|
|
| MD5 |
b4bd7d1483997f2b080dd602d0250285
|
|
| BLAKE2b-256 |
7a2e56531e43b1055894e2c6ec36794ff5a111e8977c314e0e43b17d53c7435e
|