Skip to content

Commit

Permalink
Updated docs to support Django REST Framework.
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwrona01 committed Nov 14, 2024
1 parent dea2068 commit 07b6120
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,31 @@ Example usage ajax refresh
$('#id_captcha_0').val(result['key'])
});
});

Example usage in Django REST Framework
---------------------------------

To use CAPTCHA in a serializer, simply inherit ``CaptchaSerializer``::

from captcha.serializers import CaptchaSerializer
from rest_framework import serializers

class CheckCaptchaSerializer(CaptchaSerializer):
email = serializers.EmailField()

…or use ``CaptchaModelSerializer``::

from captcha.serializers import CaptchaModelSerializer
from rest_framework import serializers

class CheckCaptchaModelSerializer(CaptchaModelSerializer):
sender = serializers.EmailField()

class Meta:
model = User
fields = ("captcha_code", "captcha_hashkey", "sender")

``CaptchaSerializer`` and ``CaptchaModelSerializer`` implement the ``captcha_code`` and ``captcha_hashkey`` fields.
In case of invalid captcha or hash code an exception is raised::

raise exceptions.ValidationError({"error": gettext_lazy("Invalid CAPTCHA")})

0 comments on commit 07b6120

Please sign in to comment.