Django Recaptcha v3 will be installed in your Django website soon, just follow my instructions. We will be using Google Recaptcha version 3 ( I am not a robot ) and a Python library django-recaptcha for this task.

This is a repost of an old blog of ours.

How to install, deploy or integrate Django recaptcha v3?

Django recaptcha v3 installation

First, install django-recaptcha in your virtual workspace.

Visit the GitHub Repo of this task/project to get all the source codes.

Install and configure django-recaptcha lib

pip install django-recaptcha

Now, add captcha in the INSTALLED_APPS section in your settings.py

captcha

Must be similar to this:

INSTALLED_APPS = [
    'captcha', # add this code

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "your_app_name", # app name
]

Recaptcha keys configuration

Add RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY at the last lines of your settings.py

RECAPTCHA_PUBLIC_KEY = 'MyRecaptchaKey123'
RECAPTCHA_PRIVATE_KEY = 'MyRecaptchaPrivateKey456'

Here, RECAPTCHA_PUBLIC_KEY means SITE KEY and RECAPTCHA_PRIVATE_KEY means SECRET KEY.

Now, go to Google reCAPTCHA admin panel and register your site for reCAPTCHA and use those data in the upper code.

Cpatcha Form configuration

Now, create a file named my_captcha.py and inside it you need to write the following Python codes:

from django import forms
from captcha.fields import ReCaptchaField

class FormWithCaptcha(forms.Form):
    captcha = ReCaptchaField()

Django views configuration

Write a context with "captcha" variable name that with render the form we have coded in FormWithCaptcha which is inside the my_captcha.py .

from django.shortcuts import render
from .my_captcha import FormWithCaptcha
# Create your views here.
def home(request):
    context = {
        "captcha": FormWithCaptcha,
    }
    return render(request, "home.html", context)

Rendering recaptcha v3

Now, we are rendering recaptcha using the Jinja template method. Use {{captcha}} for rendering the captcha form.

For now, have a look here:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <forms>

        {{captcha}}
    
    </forms>
</body>
</html>

Getting data from recaptcha form

To get data from django recaptcha, you need to use g-recaptcha-response.

get_recaptcha = request.POST.get("g-recaptcha-response")

This will return true if the checkbox is checked, else false.

Django Recaptcha v3 will be installed in your Django website soon, just follow my instructions. We will be using Google Recaptcha version 3 ( I am not a robot ) and a Python library django-recaptcha for this task.

This is a repost of an old blog of ours.

How to install, deploy or integrate Django recaptcha v3?

Django recaptcha v3 installation

First, install django-recaptcha in your virtual workspace.

Visit the GitHub Repo of this task/project to get all the source codes.

Install and configure django-recaptcha lib

pip install django-recaptcha

Now, add captcha in the INSTALLED_APPS section in your settings.py

captcha

Must be similar to this:

INSTALLED_APPS = [
    'captcha', # add this code

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "your_app_name", # app name
]

Recaptcha keys configuration

Add RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY at the last lines of your settings.py

RECAPTCHA_PUBLIC_KEY = 'MyRecaptchaKey123'
RECAPTCHA_PRIVATE_KEY = 'MyRecaptchaPrivateKey456'

Here, RECAPTCHA_PUBLIC_KEY means SITE KEY and RECAPTCHA_PRIVATE_KEY means SECRET KEY.

Now, go to Google reCAPTCHA admin panel and register your site for reCAPTCHA and use those data in the upper code.

Cpatcha Form configuration

Now, create a file named my_captcha.py and inside it you need to write the following Python codes:

from django import forms
from captcha.fields import ReCaptchaField

class FormWithCaptcha(forms.Form):
    captcha = ReCaptchaField()

Django views configuration

Write a context with "captcha" variable name that with render the form we have coded in FormWithCaptcha which is inside the my_captcha.py .

from django.shortcuts import render
from .my_captcha import FormWithCaptcha
# Create your views here.
def home(request):
    context = {
        "captcha": FormWithCaptcha,
    }
    return render(request, "home.html", context)

Rendering recaptcha v3

Now, we are rendering recaptcha using the Jinja template method. Use {{captcha}} for rendering the captcha form.

For now, have a look here:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <forms>

        {{captcha}}
    
    </forms>
</body>
</html>

Getting data from recaptcha form

To get data from django recaptcha, you need to use g-recaptcha-response.

get_recaptcha = request.POST.get("g-recaptcha-response")

This will return true if the checkbox is checked, else false.