1

I need help implementing django-recapcha in a Django project that has a custom user and also has django-allauth.

When I first set up this project I set up a custom user, as it is the recommendation from the official Django site.(https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project).

After that I implemented django-allauth for authentication.

Now I want to implement django-recapctha and I haven't found the right way to do it. I don't know how I should configure the forms.py file, because I don't understand well what is happening with this combination of custom user and django-allauth. Which is the form I should edit to configure django-recapcha and from which app?

When I configured the custom user I did it as follows:

I created an app called accounts.

I created accounts/models.py with the following code:

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    profile_pic = models.ImageField(default='default.jpg', upload_to='profile_pics')

I created accounts/forms.py with the following code:

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm


class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = get_user_model()
        fields = (
            'email',
            'username',
        )


class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = get_user_model()
        fields = (
            'email',
            'username',
        )

Then I added the following to settings.py:

AUTH_USER_MODEL = 'accounts.CustomUser'

The django-recaptcha instructions indicate that I need to add a ReCaptchaField to a form:

Instructions at https://pypi.org/project/django-recaptcha/#installation

from django import forms
from captcha.fields import ReCaptchaField

class FormWithCaptcha(forms.Form):
    captcha = ReCaptchaField()

But I don't know in which part of the project I should do it. I don't know if it is correct to do it in the forms.py file of the accounts app, where I defined the CustomUserCreationForm, or should I override the django-allauth Signup form? and if this is the case where I do it? in the accounts app where I configured the custom user model or elsewhere?

I really would appreciate any help or guidance on how to move forward with this.

  • Which form you're using `CustomUserCreationForm` or django-allauth `Signup` form if you're using both add it in both forms. – Ankit Tiwari Nov 13 '22 at 16:29
  • TBH I'm not sure what are the implications of my CustomUserCreationForm, I created it this way following a tutorial which recommends using a Custom User, in case you want to customize it later, and this was my case, because I added a picture to the model, but I think my authentication template is using django-allauth forms which are under the hood. There is why I'm a bit confused. In the project, I think my template is using django-allauth forms, because I han sign up manually or with social authentication. – Rodrigo Yanez Nov 13 '22 at 16:47

1 Answers1

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 }}