djangorecaptchadjango-allauth

How to replace super().save() in MyCustomSignupForm when recaptcha is not confirmed


How to replace super().save() in MyCustomSignupForm when recaptcha is not confirmed

class MyCustomSignupForm(SignupForm):
    def save(self, request):
        token = request.POST['g-recaptcha-response']
        action = "signup"
        r_result = recaptcha(request, token, action)
        if r_result:
            user = super(MyCustomSignupForm, self).save(request)
        else:
            messages.warning(request, '...robot...')
            user = ???
            
        return user

Solution

  • Thanks Ben. Now I have calmed down the woodpecker that kept knocking on my registration form and creating a server error.

    Code that works for me

    class MyCustomSignupForm(SignupForm):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.request = kwargs.pop('data', None)
    
        def clean(self):
            cleaned_data = super().clean()
            token = self.request.get('g-recaptcha-response')
            action = "signup"
            r_result = recaptcha(token, action)
            if not r_result:
                raise ValidationError('Are you a robot?')
            return cleaned_data
    

    p.s. Add a tag in the template

    {{ form.non_field_errors }}