欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年04月28日 10:17:47   作者:糖糖說  
這篇文章主要介紹了Django中使用pillow實現(xiàn)登錄驗證碼功能(帶刷新驗證碼功能),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

verification_code.py

import random

from PIL import Image, ImageFont, ImageDraw


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

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

    # blur the background
    for i in range(100): #這個是設置干擾線的,數(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(): #這個就是從下面這些字母里去隨機4個出來
    base = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
    result = []
    for i in range(4): #這個是4位,應該改更多位,那么上面的參數(shù)還要調試,不然顯示有問題
        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">驗證碼:</label><input type="text" id="verification_code" name="verification_code"
                                                      placeholder="Please type below code">
    <img class="identifyCode" title="點擊重新獲取" 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())"

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

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):#我這個沒跟數(shù)據(jù)庫聯(liá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')

成品如圖

在這里插入圖片描述

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

相關文章

  • python 解決mysql where in 對列表(list,,array)問題

    python 解決mysql where in 對列表(list,,array)問題

    這篇文章主要介紹了python 解決mysql where in 對列表(list,,array)問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python 異步之生成器示例詳解

    Python 異步之生成器示例詳解

    這篇文章主要為大家介紹了Python 異步之生成器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Python中使用__hash__和__eq__方法的問題

    Python中使用__hash__和__eq__方法的問題

    這篇文章主要介紹了Python中使用__hash__和__eq__方法的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Python GUI庫PyQt5樣式QSS子控件介紹

    Python GUI庫PyQt5樣式QSS子控件介紹

    這篇文章主要介紹了Python GUI庫PyQt5樣式QSS子控件介紹,需要的朋友可以參考下
    2020-02-02
  • python人工智能遺傳算法示例解析

    python人工智能遺傳算法示例解析

    這篇文章主要為大家介紹了python人工智能遺傳算法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 高考考python編程是真的嗎

    高考考python編程是真的嗎

    在本篇文章里小編給大家整理的是一篇關于高考考python編程的相關信息,有興趣的朋友們閱讀下。
    2020-07-07
  • python更換國內鏡像源三種實用方法

    python更換國內鏡像源三種實用方法

    這篇文章主要給大家介紹了關于python更換國內鏡像源三種實用方法的相關資料,更換Python鏡像源可以幫助解決使用pip安裝包時速度過慢或無法連接的問題,需要的朋友可以參考下
    2023-09-09
  • 在pycharm中為項目導入anacodna環(huán)境的操作方法

    在pycharm中為項目導入anacodna環(huán)境的操作方法

    這篇文章主要介紹了在pycharm中為項目導入anacodna環(huán)境的操作方法,本文圖文并茂通過實例詳解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • Python機器學習pytorch模型選擇及欠擬合和過擬合詳解

    Python機器學習pytorch模型選擇及欠擬合和過擬合詳解

    如何發(fā)現(xiàn)可以泛化的模式是機器學習的根本問題,將模型在訓練數(shù)據(jù)上過擬合得比潛在分布中更接近的現(xiàn)象稱為過擬合,用于對抗過擬合的技術稱為正則化
    2021-10-10
  • python 實現(xiàn)在txt指定行追加文本的方法

    python 實現(xiàn)在txt指定行追加文本的方法

    下面小編就為大家分享一篇python 實現(xiàn)在txt指定行追加文本的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04

最新評論