recaptcha is a free captcha service offered by google in order to secure websites and forms at https www google comrecaptchaadmincreate you can create new recaptcha keys and see the keys that your have already created keep in mind that recaptcha doesn t work with localhost when you create a recaptcha key your will get two separate keys clientkey secretkey clientkey should be kept in your site s front end secretkey should be kept in your site s back end an example html login form with recaptcha tag is shown below form action methodpost h2 classtextcenterlog inh2 csrftoken div classformgroup input typetext nameusername requiredrequired div div classformgroup input typepassword namepassword requiredrequired div div classformgroup button typesubmitlog inbutton div below is the recaptcha tag of html div classgrecaptcha datasitekeyclientkeydiv form below is the recaptcha script to be kept inside html tag script srchttps www google comrecaptchaapi js async deferscript below a django function for the views py file contains a login form for demonstrating recaptcha verification enter your recaptcha secret key here when method is not post direct user to login page from the frontend get username password and clientkey post recaptcha response to google s recaptcha api if the recaptcha api verified our keys authenticate the user enter your recaptcha secret key here when method is not post direct user to login page from the frontend get username password and client_key post recaptcha response to google s recaptcha api if the recaptcha api verified our keys authenticate the user
import requests

try:
    from django.contrib.auth import authenticate, login
    from django.shortcuts import redirect, render
except ImportError:
    authenticate = login = render = redirect = print


def login_using_recaptcha(request):
    
    secret_key = "secretKey"
    url = "https://www.google.com/recaptcha/api/siteverify"

    
    if request.method != "POST":
        return render(request, "login.html")

    
    username = request.POST.get("username")
    password = request.POST.get("password")
    client_key = request.POST.get("g-recaptcha-response")

    
    response = requests.post(url, data={"secret": secret_key, "response": client_key})
    
    if response.json().get("success", False):
        
        user_in_database = authenticate(request, username=username, password=password)
        if user_in_database:
            login(request, user_in_database)
            return redirect("/your-webpage")
    return render(request, "login.html")