from registration.backends.default import DefaultBackend
from registration.forms import RegistrationForm
class RecaptchaRegistrationForm(RegistrationForm)
recaptcha = ReCaptchaField(label="I'm a human")
class RecaptchaRegistrationBackend(DefaultBackend):
def get_form_class(self, request):
return RecaptchaRegistrationForm
django-registration is pretty extendable. One way to extend it is to provide a custom registration form. I'd recommend to use reCaptcha, e.g. with the widget and form field from here (archived). Then it is as simple as writing a custom form class and registration backend (which is simpler than it sounds):
from registration.backends.default import DefaultBackend
from registration.forms import RegistrationForm
class RecaptchaRegistrationForm(RegistrationForm)
recaptcha = ReCaptchaField(label="I'm a human")
class RecaptchaRegistrationBackend(DefaultBackend):
def get_form_class(self, request):
return RecaptchaRegistrationForm
The last step is to tell django-registration to use your backend. That step is described in the docs (I couldn't find a HTML version of the docs, sorry)
install it on your project. That is, copy the "captcha" folder to your project, add "captcha" to INSTALLED_APPS and add your RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY keys to settings.py too (as described in the installation instructions)
open registration/forms.py and add this field inside class RegistrationForm(forms.Form):
I've successfully setup Django Mollom and Django Simple Captcha, and the hardest part was yak shaving around installing PIL on my Mac. Implementing the code was as straightforward as the docs for each would suggest.
发布评论
评论(3)
django-registration 是相当可扩展的。扩展它的一种方法是提供自定义注册表单。我建议使用 reCaptcha,例如来自 此处 (已存档)。然后就像编写自定义表单类和注册后端一样简单(这比听起来更简单):
最后一步是告诉 django-registration 使用您的后端。 文档 (抱歉,我找不到该文档的 HTML 版本)
django-registration is pretty extendable. One way to extend it is to provide a custom registration form. I'd recommend to use reCaptcha, e.g. with the widget and form field from here (archived). Then it is as simple as writing a custom form class and registration backend (which is simpler than it sounds):
The last step is to tell django-registration to use your backend. That step is described in the docs (I couldn't find a HTML version of the docs, sorry)
我刚刚遇到了这个问题,但解决方案非常简单。
我正在使用 django-registration,并且我想要一个用于用户注册的 reCAPTCHA 字段。只需 1 分钟:
下载 django-recaptcha (
pip install django-recaptcha
)将其安装到您的项目中。也就是说,将“captcha”文件夹复制到您的项目中,将“captcha”添加到
INSTALLED_APPS
中,并将RECAPTCHA_PUBLIC_KEY
和RECAPTCHA_PRIVATE_KEY
密钥添加到设置中。 py 也(如安装说明中所述)打开
registration/forms.py
并在class RegistrationForm(forms.Form):
中添加此字段
验证码 = ReCaptchaField()
您还必须导入:
from captcha.fields import ReCaptchaField
就是这样。不到一分钟。
I've just had this problem, but the solution is dead simple.
I'm using django-registration, and I want a reCAPTCHA field for user registration. In just 1 minute:
download django-recaptcha (
pip install django-recaptcha
)install it on your project. That is, copy the "captcha" folder to your project, add "captcha" to
INSTALLED_APPS
and add yourRECAPTCHA_PUBLIC_KEY
andRECAPTCHA_PRIVATE_KEY
keys to settings.py too (as described in the installation instructions)open
registration/forms.py
and add this field insideclass RegistrationForm(forms.Form):
captcha = ReCaptchaField()
you will also have to import:
from captcha.fields import ReCaptchaField
And that's it. Less than a minute.
对于像我这样迟到的人来说,现在有很多解决方案,它们非常容易安装:
我已经成功设置了 Django Mollom 和 Django Simple Captcha,最困难的部分是在我的 Mac 上安装 PIL 的过程。实现代码就像每个文档所建议的那样简单。
For those like me arriving late to the thread, there are a bunch of solutions out there now, which are pretty easy to install:
I've successfully setup Django Mollom and Django Simple Captcha, and the hardest part was yak shaving around installing PIL on my Mac. Implementing the code was as straightforward as the docs for each would suggest.