为您的Wagtail表单添加可选的蜜罐保护。
项目描述
Wagtail Honeypot
为您的Wagtail表单添加可选的表单垃圾邮件保护
这应该有助于通过诱使机器人向应该保持空白的字段提交数据来减少表单垃圾邮件。
工作原理
当Wagtail表单提交且蜜罐保护启用时,蜜罐字段和值将出现在POST
数据中。
它为应保持为空的隐藏文本字段提供验证,并检查表单显示和提交之间的时间间隔。
如果在间隔到期之前提交表单,并且隐藏字段中有内容,则提交将被忽略。
- 不会发送电子邮件
- 不会存储提交
安装和设置
将软件包添加到您的Python环境中。
pip install wagtail-honeypot
将软件包添加到您的设置中
INSTALLED_APPS = [
...
"wagtail_honeypot",
...
]
HoneypotFormMixin 和 HoneypotFormSubmissionMixin
它们将为您的表单页面模型添加一个蜜罐启用/禁用字段,并为自定义表单提交方法提供支持。
如果您遵循官方Wagtail文档中的表单构建器说明,您的表单应该看起来像这样...
from wagtail_honeypot.models import (
HoneypotFormMixin, HoneypotFormSubmissionMixin
)
class FormField(AbstractFormField):
page = ParentalKey("FormPage", related_name="form_fields")
class FormPage(HoneypotFormMixin, HoneypotFormSubmissionMixin):
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)
content_panels = AbstractEmailForm.content_panels + [
FieldPanel("intro", classname="full"),
InlinePanel("form_fields", label="Form fields"),
FieldPanel("thank_you_text", classname="full"),
MultiFieldPanel(
[
FieldRowPanel(
[
FieldPanel("from_address", classname="col6"),
FieldPanel("to_address", classname="col6"),
]
),
FieldPanel("subject"),
],
"Email",
),
]
honeypot_panels = [
MultiFieldPanel(
[FieldPanel("honeypot")],
heading="Reduce Form Spam",
)
]
edit_handler = TabbedInterface(
[
ObjectList(content_panels, heading="Content"),
ObjectList(honeypot_panels, heading="Honeypot"),
ObjectList(Page.promote_panels, heading="Promote"),
ObjectList(Page.settings_panels, heading="Settings", classname="settings"),
]
)
如果您愿意,可以将蜜罐字段添加到内容面板中,而不是新标签页
# replace
edit_handler = TabbedInterface(
[
ObjectList(content_panels, heading="Content"),
ObjectList(honeypot_panels, heading="Honeypot"),
ObjectList(Page.promote_panels, heading="Promote"),
ObjectList(Page.settings_panels, heading="Settings", classname="settings"),
]
)
# with
content_panels = content_panels + honeypot_panels
在此处运行python manage.py makemigrations
和python manage.py migrate
蜜罐模板标签
将以下模板标签加载器添加到您的表单页面。
{% load honeypot_tags %}
在任何表单内部添加蜜罐字段模板标签
<form>
...
{% honeypot_fields page.honeypot %}
...
</form>
在您的Wagtail网站上,您现在应该能够添加一个新的表单页面,启用蜜罐字段。
测试蜜罐字段是否工作
- 查看新创建的表单页面。
- 蜜罐字段是可见的,可以用任何值提交。
- 通过将蜜罐字段设置为任何值来测试它。如果您在表单页面中启用了它,它不会保存表单提交或发送电子邮件。
隐藏蜜罐字段
当在浏览器中查看时,蜜罐字段应该是不可见的。
使用CSS & JS隐藏蜜罐字段
该包提供了一些基本的CSS和JavaScript,您可以使用它们来隐藏字段。
示例:将以下内容添加到您的表单模板中。
<!-- recommended:
to add both but you can use one or the other -->
{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/honeypot.css' %}">
{% endblock extra_css %}
<!-- alternative:
but without the css above loaded first
the field could be seen for a flash while the page loads -->
{% block extra_js %}
<script src="{% static 'js/honeypot.js' %}"></script>
{% endblock extra_js %}
该字段应该是可见隐藏的,并且不能接收来自网站访问者的任何值。
渲染时,字段将具有HTML属性
tabindex="-1" autocomplete="off"
,以防止网站访问者使用Tab键移动到该字段并禁用任何自动完成浏览器功能。
开发者文档
开发者文档提供详细信息。
版本
Wagtail蜜罐可用于以下环境
- Python 3.9+
- Django 4.2+
- Wagtail 5.1+
贡献
欢迎提供改进此包的贡献或想法。
项目详情
下载文件
下载适用于您平台的应用程序文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
wagtail_honeypot-1.2.0.tar.gz (10.7 kB 查看哈希值)