建網(wǎng)站添加付款方式中國(guó)軟文網(wǎng)官網(wǎng)
一、用pillpw生成圖片驗(yàn)證碼
1、安裝pillow
pip install pip install pillow
2、下載字體
比如:Monaco.ttf
3、實(shí)現(xiàn)生成驗(yàn)證碼的方法
該方法返回一個(gè)img ,可以把這個(gè)img圖片保存到內(nèi)存中,也可以以文件形式保存到磁盤(pán),還返回了驗(yàn)證碼的文字。
在app01->utils->code.py, code.py內(nèi)容如下:
import random
from PIL import Image,ImageDraw,ImageFont,ImageFilter
def check_code(width=120, height=30, char_length=5, font_file='Monaco.ttf', font_size=28):code = []img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))draw = ImageDraw.Draw(img, mode='RGB')def rndChar():"""生成隨機(jī)字母:return:"""return chr(random.randint(65, 90))def rndColor():"""生成隨機(jī)顏色:return:"""return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))# 寫(xiě)文字font = ImageFont.truetype(font_file, font_size)for i in range(char_length):char = rndChar()code.append(char)h = random.randint(0, 4)draw.text([i * width / char_length, h], char, font=font, fill=rndColor())# 寫(xiě)干擾點(diǎn)for i in range(40):draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())# 寫(xiě)干擾圓圈for i in range(40):draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())x = random.randint(0, width)y = random.randint(0, height)draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())# 畫(huà)干擾線for i in range(5):x1 = random.randint(0, width)y1 = random.randint(0, height)x2 = random.randint(0, width)y2 = random.randint(0, height)draw.line((x1, y1, x2, y2), fill=rndColor())img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)return img, ''.join(code)
驗(yàn)證碼的效果如下:
二、在登錄界面加上驗(yàn)證碼
1、添加一個(gè)驗(yàn)證碼的url, 在urls.py 中增加:
from django.urls import path
from app01.views import user,depart,pretty,admin,accounturlpatterns = [path('login/account/',account.login),path('logout/',account.logout),path('image/code/',account.image_code),
]
2、在視圖函數(shù)中增加驗(yàn)證碼的處理
把生成后的圖片保存再內(nèi)存中,返回給頁(yè)面,驗(yàn)證碼的文字要先保存到session中,方便后面根用戶輸入的驗(yàn)證碼進(jìn)行校驗(yàn)。
from django.shortcuts import render,redirect,HttpResponse
from django import forms
from io import BytesIO
from app01.utils.code import check_code
def image_code(request):#調(diào)用pillow模塊生成驗(yàn)證碼img,code_string = check_code()#將驗(yàn)證碼寫(xiě)入到自己的session中,一遍后續(xù)獲取驗(yàn)證碼進(jìn)行校驗(yàn)request.session['image_code'] = code_string#給session設(shè)置60秒超時(shí)request.session.set_expiry(60)#將圖片內(nèi)容寫(xiě)入到內(nèi)存中,from io import BytesIOstream = BytesIO()img.save(stream, 'png')return HttpResponse(stream.getvalue())
3、在登錄頁(yè)面增加驗(yàn)證碼的輸入框
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="{% static 'plugins/bootstrap-3.4.1/css/bootstrap.min.css' %}"><style>.account {width: 400px;border-radius: 5px;border: 1px solid #dddddd;box-shadow: 5px 5px 20px #aaa;margin-left: auto;margin-right: auto;margin-top: 100px;padding: 20px 40px;}.account h2 {margin-top: 10px;text-align: center;}</style>
</head>
<body>
<div class="account"><h2>用戶登錄</h2><form method="post" novalidate>{% csrf_token %}<div class="form-group"><label >{{ form.username.label }}</label>{{ form.username}}<span style="color:red">{{ form.username.errors.0 }}</span></div><div class="form-group"><label >{{ form.passwd.label }}</label>{{ form.passwd }}<span style="color:red">{{ form.passwd.errors.0 }}</span></div><div class="form-group"><label for="id_code">圖片驗(yàn)證碼</label><div class="row"><div class="col-xs-7">{{ form.code }}<span style="color: red">{{ form.code.errors.0 }}</span></div><div class="col-xs-5"><img id="image_code" src="/image/code/"></div></div></div><button type="submit" class="btn btn-primary">登錄</button></form></div><script src="{% static 'js/jquery-3.7.0.min.js' %}"></script>
<script src="{% static 'plugins/bootstrap-3.4.1/js/bootstrap.min.js' %}"></script>
</body>
</html>
登錄界面效果如下:
3、登錄的時(shí)候?qū)︱?yàn)證碼進(jìn)行校驗(yàn)
獲取到用戶輸入的驗(yàn)證碼的值,再?gòu)膕ession中取出之前保存的驗(yàn)證碼的值,兩者比較,如果一致就登錄,不一致就重定向到登錄界面,提示驗(yàn)證碼錯(cuò)誤。
還要注意的是,session在生成驗(yàn)證碼的時(shí)間設(shè)置了過(guò)期時(shí)間是60s, 在登錄的時(shí)候需要重寫(xiě)設(shè)置session的有效期。
在登錄的視圖函數(shù)修改如下:
def login(request):"""用戶登錄"""if request.method == 'GET':form = LoginForm()return render(request,'login.html',{'form':form})form = LoginForm(data=request.POST)if form.is_valid():#驗(yàn)證碼的校驗(yàn)# 這里為什么用pop 是因?yàn)閏leaned_data獲取到的是用戶輸入的所有信息,包括驗(yàn)證碼,但是后面用戶名密碼根數(shù)據(jù)庫(kù)校驗(yàn)的時(shí)候驗(yàn)證碼是不在數(shù)據(jù)庫(kù)的,所以這里就把驗(yàn)證碼給去掉user_input_code = form.cleaned_data.pop('code')code = request.session.get('image_code',"")if code.upper() != user_input_code.upper():form.add_error("code","驗(yàn)證碼錯(cuò)誤")return render(request,'login.html',{'form':form})#print(form.cleaned_data) #獲取到的值是一個(gè)字典{'username': 'root', 'passwd': '4233c0d596c55f18df8c99ad1ad8af4f'}#校驗(yàn)數(shù)據(jù)庫(kù)的用戶名和密碼admin_object = models.Admin.objects.filter(**form.cleaned_data).first()if not admin_object:form.add_error('passwd','用戶名或密碼錯(cuò)誤')return render(request,'login.html',{'form':form})#用戶名和密碼正確#網(wǎng)站生成隨機(jī)字符串; 寫(xiě)到用戶瀏覽器的cookie中,再寫(xiě)入到session中request.session['info'] = {'id':admin_object.id,'name':admin_object.username}#重新設(shè)置session 的有效期,為24小時(shí)request.session.set_expiry(60*60*24)return redirect('/admin/list/')return render(request,'login.html',{'form':form})