圖形驗證碼
在settings中配置連接reids
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
先新建一個檔案夾 utils 在檔案夾建一個comm.py 檔案
在comm.py 檔案中 封裝 一個連接redis的方法
from django_redis import get_redis_connection
def get_redis_conn():
conn = get_redis_connection()
return conn
在utils檔案夾中放入captcha包
在views中 寫圖形驗證碼介面
from utils.captcha.captcha import captcha
class ImageCode(APIView):
# 影像驗證
def get(self,request):
uuid = request.GET.get('uuid')
name,text,imagecode = captcha.generate_captcha()
conn = get_redis_conn()
# 圖形驗證碼存盤在redis里面 設定過期時間為1分鐘
conn.setex(uuid,60,text)
# 以圖片形式輸入
return HttpResponse(imagecode,content_type='image/jpg')
在vue中呼叫介面展示圖形驗證碼
<template>
<div>
<input type="text" name="pic_code" id="pic_code" class="msg_input">
<img :src="changeimage" alt="圖形驗證碼" class="pic_code" @click="getImage">
</div>
</template>
<script>
import axios from 'axios'
import {v4 as uuid4} from 'uuid'
export default{
data(){
return {
image:'',
changeimage:'',
}
},
methods:{
getImage(){
this.image = uuid4()
this.changeimage = 'http://127.0.0.1:8000/user/imagecode/?uuid='+this.image
},
},
created(){
this.getImage()
}
}
</script>
<style>
</style>
captcha包下載地址
提取碼是:mp9p
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/293033.html
標籤:其他
