1. 程式人生 > >註冊帳號界面,驗證碼圖片出錯

註冊帳號界面,驗證碼圖片出錯

from 碼代碼 str type t對象 代碼 oserror internal 圖片

出現情況:

  註冊帳號界面,驗證碼圖片出錯

  瀏覽器報錯,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())


  

註冊帳號界面,驗證碼圖片出錯