Package: drf-recaptcha
Django REST reCAPTCHADjango REST reCAPTCHA v2 and v3 field serializerRequirementsPython: 3.7, 3.8, 3.9, 3.10, 3.11Django: 3.2, 4.0, 4.1, 4.2DRF: 3.11, 3.12, 3.13, 3.14InstallationSign up for reCAPTCHAInstall withpip install drf-recaptchaAdd"drf_recaptcha"to yourINSTALLED_APPSsettings.Set in settingsDRF_RECAPTCHA_SECRET_KEYINSTALLED_APPS=[...,"drf_recaptcha",...,]...DRF_RECAPTCHA_SECRET_KEY="YOUR SECRET KEY"Usagefromrest_framework.serializersimportSerializer,ModelSerializerfromdrf_recaptcha.fieldsimportReCaptchaV2Field,ReCaptchaV3Fieldfromfeedback.modelsimportFeedbackclassV2Serializer(Serializer):recaptcha=ReCaptchaV2Field()...classGetOTPView(APIView):defpost(self,request):serializer=V2Serializer(data=request.data,context={"request":request})serializer.is_valid(raise_exception=True)...classV3Serializer(Serializer):recaptcha=ReCaptchaV3Field(action="example")...classV3WithScoreSerializer(Serializer):recaptcha=ReCaptchaV3Field(action="example",required_score=0.6,)...classGetReCaptchaScore(APIView):defpost(self,request):serializer=V3WithScoreSerializer(data=request.data,context={"request":request})serializer.is_valid()score=serializer.fields['recaptcha'].score...classFeedbackSerializer(ModelSerializer):recaptcha=ReCaptchaV2Field()classMeta:model=Feedbackfields=("phone","full_name","email","comment","recaptcha")defvalidate(self,attrs):attrs.pop("recaptcha")...returnattrsclassDynamicContextSecretKey(APIView):defpost(self,request):ifrequest.platform=="android":recaptcha_secret_key="SPECIAL_FOR_ANDROID"else:recaptcha_secret_key="SPECIAL_FOR_IOS"serializer=WithReCaptchaSerializer(data=request.data,context={"request":request,"recaptcha_secret_key":recaptcha_secret_key,},)serializer.is_valid(raise_exception=True)...classDynamicContextSecretKey(GenericAPIView):serializer_class=WithReCaptchaSerializerdefget_serializer_context(self):ifself.request.platform=="android":recaptcha_secret_key="SPECIAL_FOR_ANDROID"else:recaptcha_secret_key="SPECIAL_FOR_IOS"context=super().get_serializer_context()context.update({"recaptcha_secret_key":recaptcha_secret_key})returncontextclassMobileSerializer(Serializer):recaptcha=ReCaptchaV3Field(secret_key="")...SettingsDRF_RECAPTCHA_SECRET_KEY- set your Google reCAPTCHA secret key. Type: str.DRF_RECAPTCHA_DEFAULT_V3_SCORE- by default:0.5. Type: float.DRF_RECAPTCHA_ACTION_V3_SCORES- by default:{}. Type: dict. You can define specific score for each action e.g.{"login": 0.6, "feedback": 0.3}DRF_RECAPTCHA_DOMAIN- by default:www.google.com. Type: str.DRF_RECAPTCHA_PROXY- by default:{}. Type: dict. e.g.{'http': 'http://127.0.0.1:8000', 'https': 'https://127.0.0.1:8000'}DRF_RECAPTCHA_VERIFY_REQUEST_TIMEOUT- by default:10. Type: int.Priority of secret_key valuesettingsDRF_RECAPTCHA_SECRET_KEYthe argumentsecret_keyof fieldrequest.context["recaptcha_secret_key"]reCAPTCHA v3Validation is passed if the score value returned by Google is greater than or equal to required score.Required score value:0.0 - 1.0Priority of score valueIf not defined or zero in current item then value from next item.Value for action in settingsDRF_RECAPTCHA_ACTION_V3_SCORESValue in argumentrequired_scoreof fieldDefault value in settingsDRF_RECAPTCHA_DEFAULT_V3_SCOREDefault value0.5TestingSetDRF_RECAPTCHA_TESTING=Truein settings, no request to Google, no warnings,DRF_RECAPTCHA_SECRET_KEYis not required, set returning verification result in setting below.DRF_RECAPTCHA_TESTING_PASS=True|False- all responses are pass, defaultTrue.Usefrom django.test import override_settingsCreditsdjango-recaptchareCAPTCHA copyright 2012 Google.