linux系统,做验证码出现错误:OSError: cannot open resource

2020-11-27 14:37:56 浏览数 (2)

代码语言:javascript复制
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/tarena/1904/newwork/Django_websok/mainpage/views.py", line 67, in getcode_views
    image, code = get_verify_code()
  File "/home/tarena/1904/newwork/Django_websok/mainpage/views.py", line 55, in get_verify_code
    font = ImageFont.truetype('../static/fonts/arial.ttf', 40)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 261, in truetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 144, in __init__
    self.font = core.getfont(font, size, index, encoding, layout_engine=layout_engine)
OSError: cannot open resource

主要是:

代码语言:javascript复制
#file:mainpage/views.py
font = ImageFont.truetype('./mainpage/static/fonts/arial.ttf', 40)

我把字体文件arial.ttf 放到了/static/fonts/,所以路径对了就不存在问题

mainpage是我创建的APP,参考我的路径设置修改自己的路径。

0 人点赞