How to add Google reCAPTCHA to Django forms ?
Last Updated :
23 Jul, 2025
We will learn how to integrate Google reCAPTCHA into our Django forms to protect our website from spam and automated bots. We'll cover everything from registering our site with Google, setting up the necessary keys in your Django project, to adding the reCAPTCHA widget in your forms and validating it securely on the backend.
While this can be done manually, we will be using a third-party library as it makes the process much faster and simpler. Now let's make a sample contact form where we will integrate the reCaptcha.
Set Up Your Django Project
Prerequisites:
Create a new Django project and app:
django-admin startproject dj_recaptcha
cd dj_recaptcha
python manage.py startapp contact
Add the new app to your INSTALLED_APPS in dj_recaptcha/settings.py:
INSTALLED_APPS = [
...
'contact',
...
]
Register Your Site with Google reCAPTCHA
First you need register your site on the reCaptcha admin console. In the domains section add 127.0.0.1 since we are testing it out locally. Later on, you can add your production URL.
recaptcha admin consoleYou can specify whichever reCaptcha type you want, here we have selected v2 with " I'm not a robot tickbox " . You will get the API keys on form submission.
recaptcha api keysAdd reCAPTCHA Keys to Django Settings
Add your keys to settings.py:
RECAPTCHA_PUBLIC_KEY = Your_Site_Key
RECAPTCHA_PRIVATE_KEY = Your_Secret_key
Install django-recaptcha
Install the package with pip:
pip install django-recaptcha
Add the app to the INSTALLED_APPS list in settings.py
INSTALLED_APPS = [
...
'contact',
'captcha',
...
]
In contact/forms.py, create your form:
Python
from django import forms
from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Checkbox
class ContactForm(forms.Form):
email = forms.EmailField()
feedback = forms.CharField(widget=forms.Textarea)
captcha = ReCaptchaField(widget=ReCaptchaV2Checkbox)
The captcha field will be rendered as a checkbox field. If you specified a different type than the v2 checkbox while registering the site on recaptcha admin console, you need to change the widget attribute of ReCaptchaField above. If you don't specify one ReCaptchaV2Checkbox will be default There are three widgets that can be used with the ReCaptchaField class:
ReCaptchaV2Checkbox for Google reCAPTCHA V2 - Checkbox
ReCaptchaV2Invisible for Google reCAPTCHA V2 - Invisible
ReCaptchaV3 for Google reCAPTCHA V3
Create the HTML Template
Create contact/templates/contact.html:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Contact</title>
</head>
<body>
<h2>Contact Form</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
</body>
</html>
In contact/views.py:
Python
from django.shortcuts import render, HttpResponse
from .forms import ContactForm
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
return HttpResponse("Yay! you are human.")
else:
return HttpResponse("OOPS! Bot suspected.")
else:
form = ContactForm()
return render(request, 'contact.html', {'form':form})
In your project’s urls.py (e.g., dj_recaptcha/urls.py):
Python
from django.contrib import admin
from django.urls import path
from contact import views
urlpatterns = [
path('',views.contact, name='index'),
path('admin/', admin.site.urls),
]
Run and Test Your Server
Run your Django server:
python manage.py runserver
Open your browser and visit:
http://127.0.0.1:8000
recaptcha demo
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice