DEV Community

Cover image for Integrating hCaptcha with Django-allauth
Rachit Khurana
Rachit Khurana

Posted on

3 2

Integrating hCaptcha with Django-allauth

hCaptcha logo

We may face issues when someone uses bots to abuse our system and send automated signup/password reset to random people. So hCaptcha is a really good way to avoid bots.

Setting Up hCaptcha account

Installing hCaptcha

We will be using django-hCaptcha package from pypi.

  • Install it using the following command
pip install django-hCaptcha
Enter fullscreen mode Exit fullscreen mode
  • Add “hcaptcha” to your INSTALLED_APPS setting like this:
# project/settings.py

INSTALLED_APPS = [
    ...
    'hcaptcha',
]
Enter fullscreen mode Exit fullscreen mode
  • Addsitekey and secret key which we kept earlier to your settings.py file
# project/settings.py

...
HCAPTCHA_SITEKEY = '<your sitekey>'
HCAPTCHA_SECRET = '<your secret key>'
...
Enter fullscreen mode Exit fullscreen mode

Add hCaptcha to forms

# app/forms.py

from allauth.account.forms import SignupForm, ResetPasswordForm
from hcaptcha.fields import hCaptchaField


class CustomSignupForm(SignupForm):
    hcaptcha = hCaptchaField(theme='dark')
    # if the order of fields isn't as you expected ,then you can use field_order
    #field_order = ['username', 'email', 'password1', 'password2', 'hcaptcha']
    #customize this according to your form                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

class CustomForgetPassword(ResetPasswordForm):
    hcaptcha = hCaptchaField(theme='dark')
Enter fullscreen mode Exit fullscreen mode
  • Make these as the default forms by declaring them in settings.py file
# project/settings.py
...
ACCOUNT_FORMS = {
    'signup': '<app>.forms.MyCustomSignupForm',
    'reset_password': '<app>.forms.CustomForgetPassword',}
...
Enter fullscreen mode Exit fullscreen mode

All Done 🎉

Congrats

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More