简体   繁体   中英

how to have Google Captcha for 2 Django Form on One Page

I am getting bots on my website, so I want a captcha to stop bots from submitting forms.

I have a login and a register Django form on a single page.

My current code puts a captcha under each submit button, but it is not forcing the user to complete the captcha before submitting, and rather allows the user to just submit the form without filling out the captcha.

How can I make sure the user completes the captcha before submitting the form, so only real humans can login and register.

This is my current code:

<script src="https://www.google.com/recaptcha/api.js?onload=CaptchaCallback&render=explicit" async defer></script>


<script type="text/javascript">
    var CaptchaCallback = function () {
        grecaptcha.render('RecaptchaField1', {'sitekey': 'my_sitekey'});
        grecaptcha.render('RecaptchaField2', {'sitekey': 'my_sitekey'});
    };
</script>

<body>

<h2>Login</h2>

<form>
    {% csrf_token %}
    {{ login.as_p }}
    <button>Login</button>

    <div id="RecaptchaField1"></div>

</form>


<h2>Register</h2>
<form>
    {% csrf_token %}
    {{ signup.as_p}}
    <button>Register</button>

    <div id="RecaptchaField2"></div>
</form>

</body>

On the server side, in whichever views correspond to this form, you'll need to implement some kind of handling to verify whether the captcha response was valid.

It doesn't look like you're doing any of that yet, in the code you posted.

Check out the Recaptcha docs on verification for how to implement this specifically for your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM