【发布时间】:2020-02-16 21:38:21
【问题描述】:
我想将 Google Recaptcha v3 实施到一个有效的 Django 联系表单中。
我尝试按照这些教程进行操作:
https://simpleisbetterthancomplex.com/tutorial/2017/02/21/how-to-add-recaptcha-to-django-site.html
https://foxrow.com/using-recaptcha-v3-with-django
但是没有运气试图弄清楚。有人可以帮帮我吗?
Settings.py:
GOOGLE_RECAPTCHA_SECRET_KEY = '<KEY>'
HTML:
<script>
grecaptcha.ready(function() {
grecaptcha.execute('<SITE_KEY>', {action: 'contact'})
.then(function(token) {
document.getElementById("form").appendChild(document.CreateElement(`<input type="hidden" name="g-recaptcha-response" value=${token}`);
});
});
</script>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<script src='https://www.google.com/recaptcha/api.js?render=<SITE_KEY>'></script>
<div class="g-recaptcha" data-sitekey="<SITE_KEY>"></div>
<button type="submit">Submit</button>
</form>
Views.py:
def contact(request):
form_class = ContactForm
# new logic!
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
contact_name = request.POST.get(
'contact_name'
, '')
contact_email = request.POST.get(
'contact_email'
, '')
form_content = request.POST.get('content', '')
# Email the profile with the
# contact information
template = get_template('contact_template.txt')
context = {
'contact_name': contact_name,
'contact_email': contact_email,
'form_content': form_content,
}
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['GMAIL_ADDRESS'],
headers = {'Reply-To': contact_email }
)
email.send()
messages.info(request, "Success")
return render(request, 'contact.html', {
'form': form_class,
})
Forms.py:
from django import forms
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(
required=True,
widget=forms.Textarea
)
# the new bit we're adding
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.fields['contact_name'].label = "Name:"
self.fields['contact_email'].label = "Email:"
self.fields['content'].label = "Message:"
【问题讨论】:
-
您当前的代码有什么问题?您能描述一下您的代码当前正在做什么(错误、意外行为等)吗?
-
@dirkgroten 当前代码正在工作,它在提交表单时发送电子邮件。 “受 reCAPTCHA 保护”使用 html 显示在右下方。目前,views.py 中没有使用 recaptcha 逻辑,所以我需要一些指导。当我尝试按照我发布的一些教程进行操作时,我通常会收到“无效的 reCAPTCHA。请重试”
-
您在 POST 请求中提交了
g-recaptcha-response(值是令牌)。您需要从您的视图中将其提交给 Google API 以验证令牌是否有效并接收score。然后,一旦你有了分数,你就可以决定如何继续(例如分数 > 0.5 可以发送电子邮件,分数 Here's the request你应该提交。 -
@dirkgroten 请使用上面的教程查看 views.py 的 pastebin 以及更新后的 views.py -> pastebin.com/7BVVupR4 我收到以下消息:无效的 reCAPTCHA。请再试一次。成功
-
向我们展示
result(第 20 行),错误消息是您的代码中的内容,但不是 Google 发回的内容。这将有助于查看实际响应。
标签: javascript html django python-3.x recaptcha-v3