佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能)

Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能)

熱門標(biāo)簽:Win7旗艦版 百度AI接口 企業(yè)做大做強(qiáng) 電話運(yùn)營(yíng)中心 客戶服務(wù) 語音系統(tǒng) 呼叫中心市場(chǎng)需求 硅谷的囚徒呼叫中心

首先在項(xiàng)目里建立common目錄,編寫驗(yàn)證碼的函數(shù)

verification_code.py

import random

from PIL import Image, ImageFont, ImageDraw


def get_code():
    mode = 'RGB'
    bg_width = 180 #這個(gè)是驗(yàn)證碼那個(gè)框框的寬度
    bg_height = 30 #這個(gè)是驗(yàn)證碼那個(gè)框框的高度
    bg_size = (bg_width, bg_height)
    bg_color = (255, 255, 255)
    ttf_path = 'config/DejaVuSansMono.ttf'#這個(gè)是字體,從linux里扒出來餓字體
    # ttf_path = '/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf' #這個(gè)要換你服務(wù)器里有的字體才行
    img = Image.new(mode, bg_size, bg_color)
    draw = ImageDraw.Draw(img, mode)
    font = ImageFont.truetype(ttf_path, 20)#這個(gè)俺也沒懂

    # generate text
    letters = get_letters()
    for index, text in enumerate(letters):
        x = 35 * index + 10 #這個(gè)好像是調(diào)那個(gè)字符間距的
        y = 0
        draw.text((x, y), text, get_rdmcolor(), font)

    # blur the background
    for i in range(100): #這個(gè)是設(shè)置干擾線的,數(shù)值越大,干擾的越厲害
        x = random.randint(0, bg_width)
        y = random.randint(0, bg_height)
        fill = get_rdmcolor()
        draw.point((x, y), fill)
    return img, letters


def get_letters(): #這個(gè)就是從下面這些字母里去隨機(jī)4個(gè)出來
    base = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
    result = []
    for i in range(4): #這個(gè)是4位,應(yīng)該改更多位,那么上面的參數(shù)還要調(diào)試,不然顯示有問題
        result.append(random.choice(base))
    return result

def get_rdmcolor():
    return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)

模板

!DOCTYPE html>
html lang="en">
head>
    meta charset="UTF-8">
    title>Title/title>
/head>
body>
form method="POST" action="login/">
    p>用戶名:input type="text" name="user">/p>
    p>密碼:input type="text" name="pwd">/p>
    label for="verification_code">驗(yàn)證碼:/label>input type="text" id="verification_code" name="verification_code"
                                                      placeholder="Please type below code">
    img class="identifyCode" title="點(diǎn)擊重新獲取" onclick="this.setAttribute('src','verification_code?random='+Math.random())" src="{% url 'verification_code' %}" alt="verification code">
    br>
    input type="submit" value="登錄">
/form>
script>
/script>
/body>
/html>
onclick="this.setAttribute('src','verification_code?random='+Math.random())"

這個(gè) onclick事件 就是實(shí)現(xiàn)點(diǎn)擊圖片刷新驗(yàn)證碼功能 ,那為啥要加個(gè)隨機(jī)數(shù)呢,這樣就不會(huì)走瀏覽器緩存了

urls.py

from django.urls import path

from test_login_app import views

urlpatterns = [
    path('',views.index),
    path('verification_code/', views.verification_code, name='verification_code'),
    path('login/',views.login),
    path('index/',views.index2),
]

views.py

from io import BytesIO

from django.http import HttpResponse
from django.shortcuts import render, redirect

from common.verification_code import get_code


# Create your views here.

def index(request):
    return render(request, 'login.html')


def verification_code(request):
    img, letters = get_code()
    request.session['verification_code'] = ''.join(letters)
    fp = BytesIO()
    img.save(fp, 'png')
    return HttpResponse(fp.getvalue(), content_type='image/png')


def login(request):#我這個(gè)沒跟數(shù)據(jù)庫聯(lián)動(dòng),簡(jiǎn)單模擬的邏輯
    if request.method == 'POST':
        name = request.POST.get('user')
        password = request.POST.get('pwd')
        code = request.POST.get('verification_code')
        if name == 'fuck' and password == 'xxoo' and code == request.session.get('verification_code', ''):
            return redirect('/index/')
    return render(request,'login.html')


def index2(request):
    return render(request,'index.html')

成品如圖

到此這篇關(guān)于Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能)的文章就介紹到這了,更多相關(guān)Django刷新驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 用ldap作為django后端用戶登錄驗(yàn)證的實(shí)現(xiàn)
  • 給Django Admin添加驗(yàn)證碼和多次登錄嘗試限制的實(shí)現(xiàn)
  • Django --Xadmin 判斷登錄者身份實(shí)例
  • Django Session和Cookie分別實(shí)現(xiàn)記住用戶登錄狀態(tài)操作
  • django 裝飾器 檢測(cè)登錄狀態(tài)操作
  • Django用戶登錄與注冊(cè)系統(tǒng)的實(shí)現(xiàn)示例
  • Django調(diào)用百度AI接口實(shí)現(xiàn)人臉注冊(cè)登錄代碼實(shí)例
  • django使用JWT保存用戶登錄信息
  • django-利用session機(jī)制實(shí)現(xiàn)唯一登錄的例子
  • django 框架實(shí)現(xiàn)的用戶注冊(cè)、登錄、退出功能示例
  • Django實(shí)現(xiàn)前后端登錄

標(biāo)簽:山西 濟(jì)南 喀什 長(zhǎng)沙 安康 崇左 海南 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Django中使用pillow實(shí)現(xiàn)登錄驗(yàn)證碼功能(帶刷新驗(yàn)證碼功能)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    轮台县| 思南县| 岑巩县| 改则县| 上林县| 甘孜县| 济源市| 吉安县| 昌平区| 玉溪市| 乾安县| 牡丹江市| 邹城市| 富宁县| 绿春县| 阿拉善盟| 滨州市| 乡城县| 龙海市| 上蔡县| 沐川县| 峨山| 嘉义县| 武陟县| 青铜峡市| 涪陵区| 西安市| 凤翔县| 手机| 涿州市| 海南省| 乐至县| 伊川县| 莱阳市| 阿克| 康乐县| 化隆| 巴南区| 曲靖市| 山阳县| 嘉荫县|