Django框架驗(yàn)證碼用法實(shí)例分析
本文實(shí)例講述了Django框架驗(yàn)證碼用法。分享給大家供大家參考,具體如下:
驗(yàn)證碼
1、作用
- 在用戶登錄,注冊以及一些敏感操作的時候,我們?yōu)榱朔乐狗?wù)器被暴力請求,或爬蟲爬取,我們可以使用驗(yàn)證碼進(jìn)行過濾,減輕服務(wù)器的壓力。
- 驗(yàn)證碼需要使用繪圖 Pillow
- pip3 install Pillow
- 核心API
- Image
- 需要模式
- 尺寸
- 背景色
- ImageDraw
- 綁定畫布
- 模式
- 封裝了繪制的API
- text
- point
- line
- arch
- ImageFont
- 手動指定字體
2、業(yè)務(wù)流程
繪制驗(yàn)證碼圖片
background = (10,20,30) // RGB顏色
初始化畫布
image = Image.new(‘RGB',(100,50),background)
獲取畫布中畫筆對象
draw = ImageDraw.Draw(image)
繪制驗(yàn)證碼,隨機(jī)四個
font = ImageFont.truetype(‘path',size) fontcolor = (20,40,60) draw.text((x,y),'R',font,fontcolor)
返回驗(yàn)證碼內(nèi)容
# 刪除畫筆 del draw #保存圖片到BytesIO對象 Import io buf = io.BytesIO() image.save(buf,'png') #返回BytesIO中的內(nèi)容 return HttpResponse(buf.getvalue(),'image/png')
3、代碼范例
html頁面
<form method="post" action="{% url 'sitesApp:login' %}">
{% csrf_token %}
<div class="login">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">用戶名</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1" name="uName">
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">密 碼</span>
<input type="text" class="form-control" placeholder="Password" aria-describedby="basic-addon1" name="uPswd">
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">驗(yàn)證碼</span>
<input type="text" class="form-control" placeholder="Auth code" aria-describedby="basic-addon1" name="uCode">
</div>
<div class="vcode">
<img src="/app/getvcode/" id="vcode">
</div>
<input type="submit" class="loginBtn" value="登 錄"><br>
</div>
</form>
<script type="text/javascript">
$(function () {
$('#vcode').click(function () {
$(this).attr('src',"/app/getvcode"+Math.random())
})
})
</script>
views視圖
'''
生成并返回驗(yàn)證碼
'''
def getvcode(request):
# 隨機(jī)生成驗(yàn)證碼
population = string.ascii_letters+string.digits
letterlist = random.sample(population,4)
vcode = ''.join(letterlist)
# 保存該用戶的驗(yàn)證碼
request.session['vcode']=vcode
# 繪制驗(yàn)證碼
# 需要畫布,長寬顏色
image = Image.new('RGB',(176,60),color=getRandomColor())
# 創(chuàng)建畫布的畫筆
draw = ImageDraw.Draw(image)
# 繪制文字,字體所在位置
path = os.path.join(BASE_DIR,'static','fonts','ADOBEARABIC-BOLDITALIC.OTF')
font = ImageFont.truetype(path,50)
for i in range(len(vcode)):
draw.text((20+40*i,0),vcode[i],fill=getRandomColor(),font=font)
# 添加噪聲
for i in range(500):
position = (random.randint(0,176),random.randint(0,50))
draw.point(position,fill=getRandomColor())
# 返回驗(yàn)證碼字節(jié)數(shù)據(jù)
# 創(chuàng)建字節(jié)容器
buffer = io.BytesIO()
# 將畫布內(nèi)容丟入容器
image.save(buffer,'png')
# 返回容器內(nèi)的字節(jié)
return HttpResponse(buffer.getvalue(),'image/png')
# 獲取隨機(jī)顏色
def getRandomColor():
red = random.randint(0,255)
green = random.randint(0,255)
blue = random.randint(0,255)
return (red,green,blue)
希望本文所述對大家基于Django框架的Python程序設(shè)計有所幫助。
- Django自定義插件實(shí)現(xiàn)網(wǎng)站登錄驗(yàn)證碼功能
- Django驗(yàn)證碼的生成與使用示例
- python django 實(shí)現(xiàn)驗(yàn)證碼的功能實(shí)例代碼
- Django 生成登陸驗(yàn)證碼代碼分享
- django 發(fā)送手機(jī)驗(yàn)證碼的示例代碼
- 基于Django的python驗(yàn)證碼(實(shí)例講解)
- Django 登陸驗(yàn)證碼和中間件的實(shí)現(xiàn)
- Django實(shí)現(xiàn)登錄隨機(jī)驗(yàn)證碼的示例代碼
- Django框架實(shí)現(xiàn)的普通登錄案例【使用POST方法】
- 詳解Django框架中用戶的登錄和退出的實(shí)現(xiàn)
- Django框架登錄加上驗(yàn)證碼校驗(yàn)實(shí)現(xiàn)驗(yàn)證功能示例
相關(guān)文章
python使用循環(huán)打印所有三位數(shù)水仙花數(shù)的實(shí)例
今天小編就為大家分享一篇python使用循環(huán)打印所有三位數(shù)水仙花數(shù)的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python中eval()函數(shù)的詳細(xì)使用教程
eval函數(shù)在Python中具有非常重要的地位,熟練的使用eval函數(shù)能夠?yàn)槲覀兊腜ython編程提供很多的便利之處,下面這篇文章主要給大家介紹了關(guān)于Python中eval()函數(shù)的詳細(xì)使用,需要的朋友可以參考下2022-07-07
tensorflow中的數(shù)據(jù)類型dtype用法說明
這篇文章主要介紹了tensorflow中的數(shù)據(jù)類型dtype用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
python用dataframe將csv中的0值數(shù)據(jù)轉(zhuǎn)化為nan缺失值字樣
本文主要介紹了python用dataframe將csv中的0值數(shù)據(jù)轉(zhuǎn)化為nan缺失值字樣,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
Pandas中Concat與Append的實(shí)現(xiàn)與區(qū)別小結(jié)
本文主要介紹了Pandas中Concat與Append的實(shí)現(xiàn)與區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
JS設(shè)計模式之責(zé)任鏈模式實(shí)例詳解
這篇文章主要介紹了JS設(shè)計模式之責(zé)任鏈模式,結(jié)合實(shí)例形式詳細(xì)分析了責(zé)任鏈模式的概念、原理、功能、使用場景及相關(guān)操作技巧,需要的朋友可以參考下2018-02-02

