Package: django-honeypot
Django application that provides utilities for preventing automated form spam.Provides template tags, view decorators, and middleware to add and verify honeypot fields to forms.Written by James Turk with contributions by Flavio Curella and Daniel Greenfeld.Source:https://github.com/jamesturk/django-honeypot/Requirementspython >= 3.8django >= 3.2Usagesettings.pyBe sure to addhoneypottoINSTALLED_APPSin settings.py.You will almost always need to defineHONEYPOT_FIELD_NAMEwhich is the name to use for the honeypot field.  Some sophisticated bots will attempt to avoid fields named honeypot, so it may be wise to name the field something slightly more realistic such as “phonenumber” or “body2”.HONEYPOT_VALUEis an option that you can specify to populate the honeypot field, by default the honeypot field will be empty and any text entered into it will result in a failed POST.HONEYPOT_VALUEcan be a string or a callable that takes no arguments.HONEYPOT_VERIFIERis an advanced option that you can specify to validate the honeypot.  The default verifier ensures that the contents of the honeypot field matchesHONEYPOT_VALUE.  Using a combination of a callable forHONEYPOT_VALUEandHONEYPOT_VERIFIERit is possible to implement a more advanced technique such as using timestamps.Adding honeypot fields to specific forms and viewsIt is possible to add honeypot fields to specific forms and ensure that specific views check for a valid honeypotinrequest.POST.  This can be accomplished by using therender_honeypot_fieldtemplate tag:At the top of a template file include the line:{% load honeypot %}And then within any form including the tag:{% render_honeypot_field "field_name" %}will render a honeypot field named “field_name” that is hidden by default.  The name of the honeypot field will default toHONEYPOT_FIELD_NAMEif one is not provided.To ensure that the honeypot field is both present and correct you will need to usecheck_honeypotdecorator fromhoneypot.decorators:fromhoneypot.decoratorsimportcheck_honeypot@check_honeypot(field_name='hp_field_name')defpost_comment(request):...@check_honeypotdefother_post_view(request):...This decorator will ensure that a field exists inrequest.POSTthat is named ‘field_name’.@check_honeypotwithout arguments will use the defaultHONEYPOT_FIELD_NAME.Adding honeypot fields to class-based-viewsThe same as above forAdding honeypot fields to specific forms and viewsbut add the decorator to the post method making use of django’smethod_decorator.fromdjango.utils.decoratorsimportmethod_decoratorfromhoneypot.decoratorsimportcheck_honeypot@method_decorator(check_honeypot,name='post')classMyView(FormView):...Adding honeypot fields site-wideSometimes it is desirable to add honeypots to all forms site-wide.  This is particularly useful when dealing with apps that render their own forms.  For this purpose three middlewares are provided, similar in functionality to django’s own CSRF middleware.All of these middleware live inhoneypot.middleware.HoneypotResponseMiddlewareanalyzes the output of all responses and rewrites any forms that usemethod="POST"to contain a honeypot field, just as if they had started with{% render_honeypot_field %}.  Borrowing heavily fromdjango.contrib.csrf.middleware.CsrfResponseMiddlewarethis middleware only rewrites responses with Content-Type text/html or application/xhtml+xml.HoneypotViewMiddlewareensures that for all incoming POST requests to viewsrequest.POSTcontains a valid honeypot field as defined by theHONEYPOT_FIELD_NAME,HONEYPOT_VALUE, andHONEYPOT_VERIFIERsettings.  The result is the same as if every view in your project were decorated with@check_honeypot.HoneypotMiddlewareis a combined middleware that applies bothHoneypotResponseMiddlewareandHoneypotViewMiddleware, this is the easiest way to get honeypot fields site-wide and can be used in many if not most cases. The middleware needs to be listed afterCommonMiddlewarebecause the middleware changes the response. If you list it beforeCommonMiddlewarethen theContent-Lengthheader won’t reflect the changes.Customizing honeypot displayThere are two templates used by django-honeypot that can be used to control various aspects of how the honeypot functionality is presented to the user.honeypot/honeypot_field.htmlis used to render the honeypot field.  It is given two context variablesfieldnameandvalue, corresponding toHONEYPOT_FIELD_NAMEandHONEYPOT_VALUEor any overrides in effect (such as a custom field name passed to the template tag).honeypot/honeypot_error.htmlis the error page rendered when a bad request is intercepted.  It is given the context variablefieldnamerepresenting the name of the honeypot field.