简体   繁体   English

如何在一页上有 2 个 Django 表单的 Google Captcha

[英]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.我的网站上有机器人,所以我想要一个验证码来阻止机器人提交 forms。

I have a login and a register Django form on a single page.我在一个页面上有一个登录名和一个注册 Django 表单。

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.查看有关验证的 Recaptcha 文档,了解如何专门为您的应用实现此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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