Integrating hCaptcha with Django-allauth

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

First we need new hCaptcha account. If …


This content originally appeared on DEV Community and was authored by Rachit Khurana

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
  • Add “hcaptcha” to your INSTALLED_APPS setting like this:
# project/settings.py

INSTALLED_APPS = [
    ...
    'hcaptcha',
]
  • 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>'
...

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')
  • 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',}
...

All Done 🎉

Congrats


This content originally appeared on DEV Community and was authored by Rachit Khurana


Print Share Comment Cite Upload Translate Updates
APA

Rachit Khurana | Sciencx (2022-06-21T04:53:26+00:00) Integrating hCaptcha with Django-allauth. Retrieved from https://www.scien.cx/2022/06/21/integrating-hcaptcha-with-django-allauth/

MLA
" » Integrating hCaptcha with Django-allauth." Rachit Khurana | Sciencx - Tuesday June 21, 2022, https://www.scien.cx/2022/06/21/integrating-hcaptcha-with-django-allauth/
HARVARD
Rachit Khurana | Sciencx Tuesday June 21, 2022 » Integrating hCaptcha with Django-allauth., viewed ,<https://www.scien.cx/2022/06/21/integrating-hcaptcha-with-django-allauth/>
VANCOUVER
Rachit Khurana | Sciencx - » Integrating hCaptcha with Django-allauth. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/06/21/integrating-hcaptcha-with-django-allauth/
CHICAGO
" » Integrating hCaptcha with Django-allauth." Rachit Khurana | Sciencx - Accessed . https://www.scien.cx/2022/06/21/integrating-hcaptcha-with-django-allauth/
IEEE
" » Integrating hCaptcha with Django-allauth." Rachit Khurana | Sciencx [Online]. Available: https://www.scien.cx/2022/06/21/integrating-hcaptcha-with-django-allauth/. [Accessed: ]
rf:citation
» Integrating hCaptcha with Django-allauth | Rachit Khurana | Sciencx | https://www.scien.cx/2022/06/21/integrating-hcaptcha-with-django-allauth/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.