SoFunction
Updated on 2025-03-04

Django-simple-captcha implementation methods for multiple verification codes

1. Digital verification code

Configure the digital verification code (in): To use digital verification code, configuration is requireddjango - simple - captchalibrary. existIn the file, set the verification code generation function to the function that generates the digital verification code. For example:

CAPTCHA_CHALLENGE_FUNCT = '.random_digit_challenge'

This sets the verification code type to a number, anddjango - simple - captchaA digital verification code will be generated based on this configuration.

Use digital verification codes in templates and views

In the template (e.g.) The verification code picture and input box are displayed in the same way as before. For example:

<img src="{{ captcha_image_url }}" alt="Verification Code"><br>
<input type="text"  name="captcha" required><br>

In view functions (e.g.login_viewIn ) , the logic of verifying digital verification code is similar to before. After receiving the verification code submitted by the user, the stored verification code object is obtained from the database (CaptchaStore) and compare whether the verification code entered by the user and the stored verification code are consistent.

from  import CaptchaStore
#...Other codescaptcha_value = ('captcha')
captcha_key = ('captcha_key')
try:
    captcha = (hashkey = captcha_key)
    if  == captcha_value:
        # The correct logic of verification code    else:
        # Logic of verification code errorexcept :
    # Logic of verification code not exists

2. Letter verification code

Configure letter verification code

existIn, modify the verification code generation function to a function that generates the letter verification code. For example:

CAPTCHA_CHALLENGE_FUNCT = '.random_char_challenge'

You can also set parameters such as the length of the letter verification code.

For example, set the verification code length to 6:

CAPTCHA_LENGTH = 6

Processing in templates and views

In the template, no special modification is required, and the verification code picture and input box are still processed in the conventional way. In view functions, the verification logic is also unchanged becausedjango - simple - captchaThe letter verification code will be automatically generated and verified according to the configuration.

3. Arithmetic verification code

Configure arithmetic verification code

existIn the process, set the verification code generation function to the arithmetic verification code generation function. For example:

CAPTCHA_CHALLENGE_FUNCT = '.math_challenge'

This will generate an arithmetic expression as a verification code, such as "2 + 3 =?".

Special handling in templates and views

In the template, the display method is basically the same, but consider adding some tips to let the user know that this is an arithmetic verification code. For example, add a small prompt next to the verification code image:

<img src="{{ captcha_image_url }}" alt="Verification Code"><br>
<small>Please calculate the value of the arithmetic expression</small><br>
<input type="text"  name="captcha" required><br>

In view functions, the verification logic is slightly more complicated. Because the user inputs the result of the arithmetic expression, it is necessary to obtain the arithmetic expression in the stored verification code object, calculate the correct result, and then compare it with the user input. For example:

from  import CaptchaStore
import operator
#...Other codescaptcha_value = ('captcha')
captcha_key = ('captcha_key')
try:
    captcha = (hashkey = captcha_key)
    parts = ()
    if len(parts) == 3:
        op_mapping = {'+': , '-': , '*': }
        a, op, b = parts
        correct_result = str(op_mapping[op](int(a), int(b)))
        if captcha_value == correct_result:
            # The correct logic of verification code        else:
            # Logic of verification code error    else:
        # Logic of wrong verification code formatexcept :
    # Logic of verification code not exists

This is the end of this article about the various verification codes of django-simple-captcha. For more related contents of django-simple-captcha, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!