Middleware for protecting Django forms against spam using CleanTalk service.
- Install the package:
pip install cleantalk-python-antispam
- Add your CleanTalk API key to
settings.py:CLEANTALK_API_KEY = "your_api_key_here"
- Add the middleware to your
MIDDLEWARE(typically at the end)::MIDDLEWARE = [ ... 'path.to.AntiSpamMiddleware', ]
- For time submission checks, add this to your form handling:
request.session["ct_timestamp"] = int(time.time())
The middleware automatically checks all POST requests. When spam is detected:
- Sets
request.cleantalk_blockedflag with CleanTalk's response message - By default doesn't block the request (uncomment the line to enable blocking)
The middleware checks these form fields by default:
email- user's email addressmessage- message textname- user's namect_js_on- JavaScript enabled flag (1/0)
Customize the data dictionary in the middleware to adjust field mapping.
For proper form submission time checking (submit_time):
- Set the timestamp in session when displaying the form
- Ensure Django sessions are properly configured
- CleanTalk account https://cleantalk.org/register?product=anti-spam