Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 21, 2022 04:53 am GMT

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
  • Add hcaptcha to your INSTALLED_APPS setting like this:
# project/settings.pyINSTALLED_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.pyfrom allauth.account.forms import SignupForm, ResetPasswordFormfrom hcaptcha.fields import hCaptchaFieldclass 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


Original Link: https://dev.to/dilutewater/integrating-hcaptcha-with-django-allauth-24lo

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To