Django reCaptcha v3 – Install reCaptcha in Django site

Django recaptcha | Install django captcha | Django recaptcha v3

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.

How to install, deploy or integrate django recaptcha v3?

Django recaptcha v3 installation

First, install django-recaptcha in your virtual workspace.

Visit 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
]

Also check, Selenium Python tutorial for beginners

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.

Posted by Bishwas Bhandari

I am 18 years, young developer. Trying to simplify the problems of the people who are struggling around the web to provide something better in this world. I have developed Adsense Eligibility Checker Tool to help you guys to check whether your site is eligible or worthy for Google Adsense.

8 thoughts on “Django reCaptcha v3 – Install reCaptcha in Django site”

  1. Thanks brother! A lot helpful. May be this is the most easiest way to integrate django recaptch version 3 in my site.

  2. after I’ve submitted the form and go back and refresh page what will happen if this text comes again. i am trying to find a solution in django no hope redirect is not useful.

        1. I don’t wanna mess with URL parameters, so yeah. Sorry for the trouble that title is causing you.

Leave a Reply

Your email address will not be published. Required fields are marked *