views
def img_code(request):
"""生产图片验证码"""
from io import BytesIO
from utils.image_code import check_code
img,code = check_code()
# 把图片写到内存中
stream = BytesIO()
img.save(stream,'png')
return HttpResponse(stream.getvalue())
# 先生产本地,再读取,再返回
# 和上面直接在内存中效果一样
# 并且如果将临时图片存在本地 还要考虑文件重名的问题
# 使用内存 速度还更快
# with open('code.png','wb') as f:
# img.save(f,format="png")
# with open('code.png', 'rb') as f:
# data = f.read()
# return HttpResponse(data)
其中 check_code()
代码 可点击这里
访问对应视图的 url
即可获取到一张图片