SoFunction
Updated on 2024-07-15

Django Framework CAPTCHA Usage Example Analysis

This article example tells the Django framework CAPTCHA usage. Shared for your reference, as follows:

CAPTCHA, a type of challenge-response test (computing)

1. Role

  • During user login, registration and some sensitive operations, we can use CAPTCHA for filtering in order to prevent the server from being violently requested, or crawled by reptiles, and reduce the pressure on the server.
  • CAPTCHA requires the use of Mapping Pillow
    • pip3 install Pillow
    • Core API
    • Image
      • Required mode
      • sizes
      • background color
    • ImageDraw
      • Binding Canvas
      • paradigm
      • Encapsulates the drawing API
      • text
      • point
      • line
      • arch
    • ImageFont
      • Specify fonts manually

2、Business process

Drawing CAPTCHA images

background = (10,20,30) // RGB colors

Initialize Canvas

image = (‘RGB',(100,50),background)

Get the brush object in the canvas

draw = (image)

Draw CAPTCHA with four random

font = (‘path',size)
fontcolor = (20,40,60)
((x,y),'R',font,fontcolor)

Return CAPTCHA content

# Delete Brush
del draw
# Save image to BytesIO object
Import io
buf = ()
(buf,'png')
# Returns the contents of BytesIO
return HttpResponse((),'image/png')

3. Code examples

html page

<form method="post" action="{% url 'sitesApp:login' %}">
  {% csrf_token %}
    <div class="login">
      <div class="input-group">
       <span class="input-group-addon" id="basic-addon1">user ID</span>
       <input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1" name="uName">
      </div>
      <div class="input-group">
       <span class="input-group-addon" id="basic-addon1">name of an ancient state&nbsp;&nbsp;&nbsp;&nbsp;classifier for length or distance (yard), happenings etc</span>
       <input type="text" class="form-control" placeholder="Password" aria-describedby="basic-addon1" name="uPswd">
      </div>
      <div class="input-group">
       <span class="input-group-addon" id="basic-addon1">验证classifier for length or distance (yard), happenings etc</span>
       <input type="text" class="form-control" placeholder="Auth code" aria-describedby="basic-addon1" name="uCode">
      </div>
      <div class="vcode">
        <img src="/app/getvcode/" id="vcode">
      </div>
      <input type="submit" class="loginBtn" value="Register."><br>
    </div>
  </form>
  <script type="text/javascript">
    $(function () {
      $('#vcode').click(function () {
        $(this).attr('src',"/app/getvcode"+())
      })
    })
  </script>

views view

'''
Generate and return CAPTCHA
'''
def getvcode(request):
  # Randomly generated CAPTCHA
  population = string.ascii_letters+
  letterlist = (population,4)
  vcode = ''.join(letterlist)
  # Save the authentication code for this user
  ['vcode']=vcode
  # Drawing CAPTCHA
  # Need canvas, width and color #
  image = ('RGB',(176,60),color=getRandomColor())
  # Create brushes for the canvas
  draw = (image)
  # Draw the text, where the font is located
  path = (BASE_DIR,'static','fonts','')
  font = (path,50)
  for i in range(len(vcode)):
    ((20+40*i,0),vcode[i],fill=getRandomColor(),font=font)
  # Add noise
  for i in range(500):
    position = ((0,176),(0,50))
    (position,fill=getRandomColor())
  # Return CAPTCHA byte data
  # Create byte containers
  buffer = ()
  # Drop the contents of the canvas into the container
  (buffer,'png')
  # Returns the bytes in the container
  return HttpResponse((),'image/png')
# Get random colors
def getRandomColor():
  red = (0,255)
  green = (0,255)
  blue = (0,255)
  return (red,green,blue)

I hope that what I have said in this article will help you in designing Python programs based on Django framework.