How to add a captcha in a Django website?

Captcha is one of the modern ways used for verification in websites; it is very cool way and every second website is using it. You can use Google captcha but it is really a headache to apply it; however in Django, we have a simpler method to do so.

In this article, we are going to learn how to create a captcha in a Django website. So, let's get started.


First of all, create a Django project and an app.

Now install the django-simple-captcha library −

pip install django-simple-captcha

Go to and inside INSTALLED_APPS, add your app and "captcha": −

   "captchaproject", #My app name
   "captcha" # the module name

It will add captcha as an app.

In project's root directory, add the following lines −

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
   path('', include("captchaproject.urls")),

It will add url for my app and the captcha url where verification will occur.

In app's main directory, create a and type −

from django import forms
from captcha.fields import CaptchaField

class MyForm(forms.Form):

Here we created a form with captcha field.

Don't forget to run "python migrate"

Now in app's, add the following lines −

from django.shortcuts import render
from .forms import MyForm
# Create your views here.
def home(request):
   if request.method=="POST":
      if form.is_valid():
   return render(request,"home.html",{"form":form})

We render the form in home view and in POST handler, we verify the form or we can say captcha and after verification, we print the result.

Create an HTML file in the templates directory (directory where you add every HTML or CSS file that you render, I assume you know and already configure it) and add the following lines −

<!DOCTYPE html>
      <form method="POST" novalidate>
         <input type="submit" value="submit">

This is the frontend where we are rendering the captcha form.

We are done; now you can proceed to check the output.