1

I have a DJANGO application where I protect forms with reCAPTCHA v2 invisible. I have no issue protecting 1 form, everything works well and I have been using this feature for quite some time.

However, if I have 2 forms on the same page (e.g. in separate modal windows), EACH protected with their own captcha, it doesn't work.

Here's what happens:

  • OK: both forms have their individual onSubmit_xxx and onSubmit_yyy function (with a different widget uuid)
  • OK: both forms get their "submit" event properly redirected to the verifyCaptcha_xxx or verifyCaptcha_yyy function
  • NOK: after grecaptcha.execute(), ALWAYS the onSubmit_xxx function (of the first form) is executed (with the wrong widget uuid) and therefore the wrong form is submitted

I am using Django 2.1.3 with Python 3.5 and django-recaptch v2.0.6

Any hints? Thanks a lot!!


As requested some further information on how I implemented form, view and template:

app1: forms.py

from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Invisible

form1_captcha = ReCaptchaField(widget=ReCaptchaV2Invisible(
        attrs={
            'data-callback': 'submit_form1',
        }
))

app2: forms.py

...
form2_captcha = ReCaptchaField(widget=ReCaptchaV2Invisible(
        attrs={
            'data-callback': 'submit_form2',
        }
))

and so on.

app_n: views.py | n=1, 2...

form_class = app<n>Form  
return render(request, 'index.html', {'app_<n>_form':form_class,})

template:

<form id="form_<n>" role="form" action="" method="post">
{% csrf_token %}
{{ app_<n>_form.captcha }}
                      
<script>
  // For IE 11, bind "closest" function to DOM Element for compatibility                                        
  $(".g-recaptcha")[0].closest = function(a) { 
    return $(this).closest(a)[0];                                           
  }
</script>
</form>




EvilSmurf
  • 819
  • 1
  • 7
  • 21

1 Answers1

0

I had a similar problem. The django-recaptcha package doesn't allow to show multiple recaptchas on a single page. I solved it following this:

https://github.com/torchbox/django-recaptcha/issues/75#issuecomment-343979279

LaCharcaSoftware
  • 1,075
  • 1
  • 5
  • 13
  • thank you LaCharcaSoftware, I have already seen that and I am indeed using this "solution" right now, but with it's drawbacks. There are some issues with this solution having the challenge frame outside the form element (sometimes it gets stuck with timeouts and stuff) - also I cannot style the position of individual challenge frames as they have no ID or anything to link them to their appropriate form, meaning I have 5 forms thus 5 frames and no way of telling which frame belongs to which form - i REALLY would like to get a solution working with django-recaptcha itself – EvilSmurf Feb 20 '22 at 23:44
  • where does it say "it doesn't allow to show multiple recaptchas on a single page"? – EvilSmurf Feb 20 '22 at 23:45
  • I’m not really sure where i found it. Today I only found this reference: https://github.com/arteria/cmsplugin-contact-plus/issues/29#issuecomment-168980265 – LaCharcaSoftware Feb 21 '22 at 00:01
  • "invi84 commented on 30 Aug 2019: Seems to work meanwhile"... – EvilSmurf Feb 21 '22 at 16:02