[英]Django simple math captcha , how to pass custom string as argument?
我从这里使用django-simple-math-captcha插件: https : //github.com/alsoicode/django-simple-math-captcha
我已经成功配置了它,并且可以正常工作。 实际上这是我的代码:
from simplemathcaptcha.fields import MathCaptchaField
class ContactForm(Form, FormMixin):
captcha = MathCaptchaField()
根据官方文档,如果您想更改上述验证码中的某些默认设置,则只需将其作为参数传递即可。 例如,我想更改默认问题字符串,并且在文档中说:
(可选)您可以将以下参数传递给该字段以对其进行配置。
....
question_tmpl
带有占位符格式的字符串,用于显示的问题。
默认值:“%(num1)i%(运算符)s%(num2)i是什么?”
...
我试图以这种方式做:
captcha = MathCaptchaField(question_tmpl="What is the result of %(num1)i %(operator)s %(num2)i?")
在我添加了此更改之后,django给了我一个错误
**TypeError at /contact/**
__init__() got an unexpected keyword argument 'question_tmpl'
通过以下论点的正确方法是什么?
通过阅读代码 ,我相信该文档是错误的。 当前,您需要使用MathCaptchaWidget
:
captcha = MathCaptchaField(widget=MathCaptchaWidget(
question_tmpl="What is the result of %(num1)i %(operator)s %(num2)i?"))
如果您同时使用django-allauth和django-simple-math-captcha,并且需要国际化:
settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'your_app.forms.AllauthSignupForm'
表格
from simplemathcaptcha.fields import MathCaptchaField
from simplemathcaptcha.widgets import MathCaptchaWidget
class AllauthSignupForm(forms.Form):
captcha=MathCaptchaField(widget=MathCaptchaWidget(question_tmpl=_('What is %(num1)i %(operator)s %(num2)i ? ')),
error_messages = {'invalid': _('Please check your math and try again.'), 'invalid_number': _('Enter a whole number.')})
def signup(self, request, user):
""" Required, or else it throws <The custom signup form must implement a "signup" method>"""
pass
signup.html
{{ form.captcha }} {{ form.captcha.errors }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.