[Answered ]-Implementing django-recaptcha in Django project with a custom user and django-allauth

1👍

So far the solution to this question is as follows, at least it seems to be working now.

Override the Signup form of django-allauth to include a captcha field, and implement django-recaptcha.

accounts/form.py

from allauth.account.forms import SignupForm
from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Checkbox

class CustomSignupForm(SignupForm):

    captcha = ReCaptchaField(widget=ReCaptchaV2Checkbox())

    def save(self, request):

        user = super(CustomSignupForm, self).save(request)

        return user

settings.py:

INSTALLED_APPS = [
    ...
    'captcha',
    ...
]

ACCOUNT_FORMS = {'signup': 'accounts.forms.CustomSignupForm'}

RECAPTCHA_PUBLIC_KEY = 'site key'
RECAPTCHA_PRIVATE_KEY = 'private key'

SILENCED_SYSTEM_CHECKS = ['captcha.recaptcha_test_key_error']

signup.html:

{{ form.captcha.errors }}
{{ form.captcha }}

Leave a comment