Skip to content

Instantly share code, notes, and snippets.

@ihfazhillah
Created May 22, 2019 06:28
Show Gist options
  • Save ihfazhillah/9680f6f3ba3fcb86dd9496c9fe90f133 to your computer and use it in GitHub Desktop.
Save ihfazhillah/9680f6f3ba3fcb86dd9496c9fe90f133 to your computer and use it in GitHub Desktop.
google recaptcha
// 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>
RECAPTCHA_SITE_KEY = "your site key"
RECAPTCHA_SECRET_KEY = "your secret key"
from django.conf import settings
def index(request):
"""
simple render index.html
"""
return render(request, 'index.html', {'site_key': settings.RECAPTCHA_SITE_KEY})
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