如何在django中使用captcha来修复此错误



我的代码(python版本=3.10,django版本=4.0(:

def get_code(request):
img = ImageCaptcha(200, 100)
im = img.generate_image(chars='1234')
fp = BytesIO()
im.save(fp, 'png')
return HttpResponse(fp.getvalue(), content_type='image/png')

错误:

TypeError at /get_code/
'float' object cannot be interpreted as an integer
Request Method: GET
Request URL:    http://localhost:8000/get_code/
Django Version: 4.0
Exception Type: TypeError
Exception Value:    
'float' object cannot be interpreted as an integer

试试看:

return HttpResponse(int(fp.getvalue()), content_type='image/png')

这种方式:

def get_code(request):
img = ImageCaptcha(200, 100)
im = img.generate_image(chars='1234')
response = HttpResponse(content_type='image/png')
im.save(response, 'PNG')
return response

最新更新