如何在Django中验证Google reCAPTCHA v2 [英] How to validate Google reCAPTCHA v2 in django
问题描述
我一直在尝试在自己制作的网站上使用Google reCAPTCHA.验证码加载在网页上,但是我无法使用几种方法对其进行验证.我已经尝试使用在给出的方法进行Recaptcha验证如何使用Python插件reCaptcha客户端进行验证?但是我认为它已经过时了,因为它不再起作用了,它指的是挑战,而我正在尝试使用的是Google全新的复选框" reCAPTCHA v2,或者我可能需要在安装recaptcha-client之后更改设置或django-recaptcha.
I have been trying to use the Google reCAPTCHA on a website that I've been making. The captcha loads on the webpage but I've been unable to validate it using several methods. I've tried the recaptcha validation using the method given at How to use Python plugin reCaptcha client for validation? but I think it's outdated as it no longer works and it is referring to challenges whereas the one I'm trying to use is the new 'checkbox' reCAPTCHA v2 by Google or maybe I need to make changes in my settings after installing recaptcha-client or django-recaptcha.
请帮助!
推荐答案
这里有一个第三方Django应用程序来实现新的reCAPTCHA v2:
There is a third-party Django app to implement the new reCAPTCHA v2 here:
https://github.com/ImaginaryLandscape/django-nocaptcha-recaptcha
安装后,将以下行添加到以下文件:
After installing it, add the following lines to the following files:
# settings.py
NORECAPTCHA_SITE_KEY = <the Google provided site_key>
NORECAPTCHA_SECRET_KEY = <the Google provided secret_key>
INSTALLED_APPS = (
....
'nocaptcha_recaptcha'
)
#forms.py
from nocaptcha_recaptcha.fields import NoReCaptchaField
class YourForm(forms.Form):
.....
captcha = NoReCaptchaField()
# In your template, add the following script tag:
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
这篇关于如何在Django中验证Google reCAPTCHA v2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!