Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add a captcha in a Django website?
CAPTCHA is a security feature used to verify that users are human, not bots. Django provides an easy way to implement CAPTCHA using the django-simple-captcha library, which generates image-based challenges for user verification.
In this article, we'll learn how to add CAPTCHA functionality to a Django website step by step.
Installation
First, install the required library ?
pip install django-simple-captcha
Django Configuration
Settings Configuration
Add the CAPTCHA app to your settings.py ?
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'captchaproject', # Your app name
'captcha', # CAPTCHA module
]
URL Configuration
In your project's main urls.py, include the CAPTCHA URLs ?
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('captchaproject.urls')),
path('captcha/', include('captcha.urls'))
]
Creating the CAPTCHA Form
Create a forms.py file in your app directory ?
from django import forms
from captcha.fields import CaptchaField
class ContactForm(forms.Form):
name = forms.CharField(max_length=100)
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea)
captcha = CaptchaField()
Database Migration
Run migrations to create CAPTCHA tables ?
python manage.py migrate
Creating the View
In your views.py, create a view to handle the form ?
from django.shortcuts import render
from django.http import HttpResponse
from .forms import ContactForm
def contact_view(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# CAPTCHA validation passed
return HttpResponse('Form submitted successfully!')
else:
# CAPTCHA validation failed
return render(request, 'contact.html', {'form': form})
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
Creating the Template
Create a contact.html template ?
<!DOCTYPE html>
<html>
<head>
<title>Contact Form with CAPTCHA</title>
<style>
.form-group { margin: 15px 0; }
label { display: block; margin-bottom: 5px; }
input, textarea { width: 300px; padding: 5px; }
.captcha { margin: 10px 0; }
</style>
</head>
<body>
<h2>Contact Us</h2>
<form method="POST">
{% csrf_token %}
<div class="form-group">
{{ form.name.label_tag }}
{{ form.name }}
</div>
<div class="form-group">
{{ form.email.label_tag }}
{{ form.email }}
</div>
<div class="form-group">
{{ form.message.label_tag }}
{{ form.message }}
</div>
<div class="form-group captcha">
{{ form.captcha.label_tag }}
{{ form.captcha }}
</div>
<input type="submit" value="Submit">
</form>
</body>
</html>
URL Routing
Create a urls.py in your app directory ?
from django.urls import path
from . import views
urlpatterns = [
path('', views.contact_view, name='contact'),
]
How It Works
The CAPTCHA system works as follows:
- Image Generation: Django generates a random image with distorted text
- User Input: Users must type the text they see in the image
- Validation: Django compares the user input with the expected value
- Security: Each CAPTCHA is unique and expires after use
Output
When you run the server and visit the contact page, you'll see a form with a CAPTCHA image. Users must enter the text shown in the image to submit the form successfully.
Conclusion
Django's simple-captcha library provides an easy way to add CAPTCHA functionality to forms. It generates secure image-based challenges that help prevent automated bot submissions while maintaining good user experience.
