注册帐号界面,验证码图片出错

出现情况:

  注册帐号界面,验证码图片出错

  浏览器报错,GET http://127.0.0.1:8000/check_code.html 500 (Internal Server Error)

  pycharm报错: OSError: cannot open resource

原因以及解决办法:

原因:

  一般出现这种情况,是因为font对象的时候写上字体路径不对

  check_code里的font_type="Monaco.ttf"

  引入的check_code模块,需要引入Monaco.tff字体

  另外补充:

     1.check_code模块还依赖PIL模块,需要先行安装

     2.生成验证码代码如下: 

  from io import BytesIO
  from utils.check_code import create_validate_code
  from django.shortcuts import HttpResponse
  def check_code(request):
   #从内存中虚拟
   stream = BytesIO()
   img, code = create_validate_code()
  img.save(stream, 'PNG')
  request.session['CheckCode'] = code
  return HttpResponse(stream.getvalue())

  

原文地址:https://www.cnblogs.com/tsinghuama/p/9345926.html