在表单django-allauth应用程序的末尾显示Google reCAPTCHA,可以通过以下步骤实现:
# settings.py
# 添加 reCAPTCHA 密钥
RECAPTCHA_PUBLIC_KEY = 'your-recaptcha-public-key'
RECAPTCHA_PRIVATE_KEY = 'your-recaptcha-private-key'
# 启用 reCAPTCHA
ACCOUNT_SIGNUP_FORM_CLASS = 'yourapp.forms.CustomSignupForm'
# forms.py
from allauth.account.forms import SignupForm
from captcha.fields import ReCaptchaField
class CustomSignupForm(SignupForm):
captcha = ReCaptchaField()
<!-- signup.html -->
<form method="post" action="{% url 'account_signup' %}">
{% csrf_token %}
{{ form.as_p }}
<div class="g-recaptcha" data-sitekey="{{ RECAPTCHA_PUBLIC_KEY }}"></div>
<button type="submit">注册</button>
</form>
<!-- 引入 reCAPTCHA JavaScript -->
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
pip install django-recaptcha
完成以上步骤后,表单django-allauth应用程序的末尾将显示Google reCAPTCHA,用户在注册时需要完成reCAPTCHA验证。这有助于防止恶意机器人注册。
领取专属 10元无门槛券
手把手带您无忧上云