图形验证码:
views:
```
from django_redis import get_redis_connection
from libs.captcha.captcha import captcha
class ImageCodeView(APIView):
def get(self, request):
# 1.接收数据
uuid = request.GET.get('uuid')
# 2.校验数据
if not uuid:
return HttpResponseForbidden('uuid无效')
# 3.处理业务
# 获取图片文本内容和图片二进制代码
text, image = captcha.generate_captcha()
# 4.把uuid和图片文本存入redis
redis_client = get_redis_connection('img_code') # 获取redis客户端
# 5.写入redis(是字符串)
redis_client.setex(uuid, 60 * 5, text)
# 6.返回响应图片
return HttpResponse(image, content_type='image/jpg')
```
libs下的captcha包:
https://pan.baidu.com/s/1HRQsk29G8QiW8gsrKmayqQ 提取码:7uoo
前端生成UUID的方法,写于medthods中:
```
getUuid() {
var d = new Date().getTime()
if (window.performance && typeof window.performance.now === 'function') {
d += performance.now()
}
var uuid