Everything up to version 0.2.2
is compatible with python 2.7, releases after 0.2.2 will
only be compatible with python 3+
>>> pip install django-g-recaptcha
if you need python 2.7...
pip install django-g-recaptcha==0.2.2
Settings.py:
...
GOOGLE_RECAPTCHA_SITE_KEY = 'key_obtained_from_google'
GOOGLE_RECAPTCHA_SECRET_KEY = 'key_obtained_from_google'
views.py:
from django.conf import settings
def view(request):
context = {
'GOOGLE_RECAPTCHA_SITE_KEY': settings.GOOGLE_RECAPTCHA_SITE_KEY,
}
return render(request, 'template.html', context)
<head>
of your HTML fileyour_template.html:
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<div class="g-recaptcha" data-sitekey="{{ GOOGLE_RECAPTCHA_SITE_KEY }}"></div>
from g_recaptcha.validate_recaptcha import validate_captcha
@validate_captcha
def view(request):
...
INSTALLED_APPS += ('g_recaptcha')
On a successful captcha submission it will process the rest of the view, if it fails, it will render a template which says there was a problem with the captcha. Override or extend this template however you see fit. It also has some logic to detect whether the request is ajax, if it is ajax it will return a simple HttpResponse that can be input on the page instead of returning a full template
If you find that you need to get around the wrapper for unittesting, there is an attribute that
contains the original view function that was passed to g_recaptcha
. It can be called like this...
Whatever you wrapped viewname is, add ._original
before the arguments
wrapped_view._original(request)