[Django]-Django Contact Form With Google Recaptcha v3

2👍

I encountered the same problem today.
It seems the javascript snippet is incorrect:

  1. Your element needs an id. (Mine is “cform” now.)
  2. CreateElement does not exist, the “c” is lower case.
  3. I could not create an element with attributes, I had to do it in several steps.

I don’t usually code in javascript so I don’t know the best practices, but here is what worked for me:

<script src='https://www.google.com/recaptcha/api.js?render=<KEY>'></script>
<script>
    grecaptcha.ready(function() {
        grecaptcha.execute(<KEY>, {action: 'contact'})
        .then(function(token) {
            ginput = document.createElement('input');
            ginput.type = "hidden";
            ginput.name = "g-recaptcha-response";
            ginput.value = token;
            document.getElementById("cform").appendChild(ginput);
        });
    });
</script>

Leave a comment