Created
May 22, 2019 06:28
-
-
Save ihfazhillah/9680f6f3ba3fcb86dd9496c9fe90f133 to your computer and use it in GitHub Desktop.
google recaptcha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1 | |
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" | |
crossorigin="anonymous"></script> | |
// 2 | |
<script src="https://www.google.com/recaptcha/api.js?render=reCAPTCHA_site_key"></script> | |
<script> | |
// 3 | |
grecaptcha.ready(function() { | |
// 4 | |
$('#contactform').submit(function(e){ | |
var form = this; | |
// 5 | |
e.preventDefault() | |
grecaptcha.execute('reCAPTCHA_site_key', {action: 'contactform'}).then(function(token) { | |
// 6 | |
$('#recaptcha').val(token) | |
// 7 | |
form.submit() | |
}); | |
}) | |
}); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RECAPTCHA_SITE_KEY = "your site key" | |
RECAPTCHA_SECRET_KEY = "your secret key" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
def index(request): | |
""" | |
simple render index.html | |
""" | |
return render(request, 'index.html', {'site_key': settings.RECAPTCHA_SITE_KEY}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
secret_key = settings.RECAPTCHA_SECRET_KEY | |
# captcha verification | |
data = { | |
'response': data.get('g-recaptcha-response'), | |
'secret': secret_key | |
} | |
resp = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data) | |
result_json = resp.json() | |
print(result_json) | |
if not result_json.get('success'): | |
return render(request, 'contact_sent.html', {'is_robot': True}) | |
# end captcha verification |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment