【问题标题】:Is there any solutions to add captcha to Django-allauth?是否有任何解决方案可以将验证码添加到 Django-allauth?
【发布时间】:2014-05-05 02:29:44
【问题描述】:

是否有任何解决方案可以将验证码与 django-allauth 一起使用? 我想在注册表单上使用验证码进行标准电子邮件+密码注册。

【问题讨论】:

    标签: captcha django-allauth


    【解决方案1】:

    接受的答案大部分都很好,但在提交注册表单时为我产生了这个错误:

    save() missing 1 required positional argument: 'user'
    

    要解决此问题,您的自定义表单可能如下所示

    class AllAuthSignupForm(forms.Form):
    
        captcha = ReCaptchaField()
    
        def signup(self, request, user):
            pass
    

    注意:如果自定义注册表单没有 signup 方法,django-allauth 也会发出警告。

    【讨论】:

      【解决方案2】:

      你也可以看看Form.field_order

      因此,使用 django-allauth 的简单注册表单,带有验证码和按您希望排序的字段,如下所示:

      from allauth.account.forms import SignupForm
      from captcha.fields import ReCaptchaField
      
      
      class MyCustomSignupForm(SignupForm):
          captcha = ReCaptchaField()
      
          field_order = ['email', 'password1', 'captcha']
      

      在这种情况下,验证码将位于最后。

      【讨论】:

        【解决方案3】:

        我也需要用 django-allauth 来做这件事,发现实现 django-recaptcha 包相对简单。

        配置 django-recaptcha

        注册recaptcha account

        插入您的设置

        # settings.py
        
        RECAPTCHA_PUBLIC_KEY = 'xyz'
        RECAPTCHA_PRIVATE_KEY = 'xyz'
        
        RECAPTCHA_USE_SSL = True     # Defaults to False
        

        自定义注册表单

        安装 django-recaptcha 后,我跟着 someguidelines 自定义了 SignupForm。

        from django import forms
        from captcha.fields import ReCaptchaField
        
        class AllAuthSignupForm(forms.Form):
        
            captcha = ReCaptchaField()
        
            def save(self, request, user):
                user = super(AllAuthSignupForm, self).save(request)
                return user
        

        你还需要在settings.py中告诉allauth继承这个表单

        ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.AllAuthSignupForm'
        

        连接注册表单模板

        {{ form.captcha }}{{ form.captcha.errors }} 此时应该在注册模板上下文中可用。

        就是这样!似乎所有验证逻辑都隐藏在 ReCaptchaField 中。

        【讨论】:

        • 我已尝试使用最新版本的 django-allauth,它不需要更改模板。只需在模型中添加新的验证码字段即可。
        • 太棒了。有什么办法可以在最后添加验证码字段。我正在使用 django-allauth 应用程序。
        • @PrithvirajMitra 您最后是否设法获得了验证码字段?我也遇到了同样的问题。
        • @Zorgan 否(使用 django-allauth)。所以我不得不使用 django-recaptcha 包,然后在 forms.py 的 signupform 中输入captcha = ReCaptchaField(attrs={'theme' : 'clean'})。在此之前,我必须导入 from captcha.fields import ReCaptchaField
        • django-recaptcha 不允许验证码是可选的,所以我推出了自己的服务器端验证码解决方案github.com/praekelt/django-recaptcha/issues/217
        【解决方案4】:

        要将 ReCaptcha 字段置于表单底部,只需在验证码字段之前添加其他字段即可。 那么user, email, captcha, password1, password2 变成 user, email, password1, password2, captcha 的形式如下:

        from allauth.account.forms import SignupForm, PasswordField
        from django.utils.translation import ugettext_lazy as _
        from captcha.fields import ReCaptchaField
        
        class UpdatedSignUpForm(SignupForm):
            password1 = PasswordField(label=_("Password"))
            password2 = PasswordField(label=_("Password (again)"))
            captcha = ReCaptchaField()
        
            def save(self, request):
                user = super(UpdatedSignUpForm, self).save(request)
                return user
        

        然后您只需将该表单添加到settings.py 文件中,如上一个答案中所述。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-13
          • 1970-01-01
          • 2012-04-14
          • 1970-01-01
          • 2021-12-15
          • 2016-08-20
          相关资源
          最近更新 更多