温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Django框架怎么实现验证码功能

发布时间:2021-05-12 17:09:51 来源:亿速云 阅读:87 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用Django框架怎么实现验证码功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

验证码

1、作用

  • 在用户登录,注册以及一些敏感操作的时候,我们为了防止服务器被暴力请求,或爬虫爬取,我们可以使用验证码进行过滤,减轻服务器的压力。

  • 验证码需要使用绘图 Pillow

    • 手动指定字体

    • 绑定画布

    • 模式

    • 封装了绘制的API

    • text

    • point

    • line

    • arch

    • 需要模式

    • 尺寸

    • 背景色

    • pip3 install Pillow

    • 核心API

    • Image

    • ImageDraw

    • ImageFont

2、业务流程

绘制验证码图片

background = (10,20,30) // RGB颜色

初始化画布

image = Image.new(‘RGB',(100,50),background)

获取画布中画笔对象

draw = ImageDraw.Draw(image)

绘制验证码,随机四个

font = ImageFont.truetype(‘path',size)
fontcolor = (20,40,60)
draw.text((x,y),'R',font,fontcolor)

返回验证码内容

# 删除画笔
del draw
#保存图片到BytesIO对象
Import io
buf = io.BytesIO()
image.save(buf,'png')
#返回BytesIO中的内容
return HttpResponse(buf.getvalue(),'image/png')

3、代码范例

html页面

<form method="post" action="{% url 'sitesApp:login' %}">
  {% csrf_token %}
    <div class="login">
      <div class="input-group">
       <span class="input-group-addon" id="basic-addon1">用户名</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">密&nbsp;&nbsp;&nbsp;&nbsp;码</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">验证码</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="登 录"><br>
    </div>
  </form>
  <script type="text/javascript">
    $(function () {
      $('#vcode').click(function () {
        $(this).attr('src',"/app/getvcode"+Math.random())
      })
    })
  </script>

views视图

'''
生成并返回验证码
'''
def getvcode(request):
  # 随机生成验证码
  population = string.ascii_letters+string.digits
  letterlist = random.sample(population,4)
  vcode = ''.join(letterlist)
  # 保存该用户的验证码
  request.session['vcode']=vcode
  # 绘制验证码
  # 需要画布,长宽颜色
  image = Image.new('RGB',(176,60),color=getRandomColor())
  # 创建画布的画笔
  draw = ImageDraw.Draw(image)
  # 绘制文字,字体所在位置
  path = os.path.join(BASE_DIR,'static','fonts','ADOBEARABIC-BOLDITALIC.OTF')
  font = ImageFont.truetype(path,50)
  for i in range(len(vcode)):
    draw.text((20+40*i,0),vcode[i],fill=getRandomColor(),font=font)
  # 添加噪声
  for i in range(500):
    position = (random.randint(0,176),random.randint(0,50))
    draw.point(position,fill=getRandomColor())
  # 返回验证码字节数据
  # 创建字节容器
  buffer = io.BytesIO()
  # 将画布内容丢入容器
  image.save(buffer,'png')
  # 返回容器内的字节
  return HttpResponse(buffer.getvalue(),'image/png')
# 获取随机颜色
def getRandomColor():
  red = random.randint(0,255)
  green = random.randint(0,255)
  blue = random.randint(0,255)
  return (red,green,blue)

上述就是小编为大家分享的使用Django框架怎么实现验证码功能了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI