码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-21 14:29:01      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:.sh   mon   def   stream   补充   val   byte   出错   col   

出现情况:

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

  浏览器报错,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())

 


  

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

标签:.sh   mon   def   stream   补充   val   byte   出错   col   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!