python 全栈开发,Day85(Git使用进阶,随机生成图片验证码)

一、Git使用进阶

因为时间关系,所以没有时间写!见谅

二、随机生成图片验证码

Python随机生成图片验证码,需要使用PIL模块.

安装:

pip3 install pillow

因为时间关系,不一一演示了,直接上代码!

修改urls.py,增加路径code

from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', views.login),
    path('code/', views.code),
]
View Code

修改views.py,增加视图函数

from django.shortcuts import render,HttpResponse,redirect
from utils.code import check_code

def code(request):
    """
    生成图片验证码
    :param request:
    :return:
    """
    img,random_code = check_code()
    request.session['random_code'] = random_code
    from io import BytesIO
    stream = BytesIO()
    img.save(stream, 'png')
    return HttpResponse(stream.getvalue())

def login(request):
    """
    用户登陆
    :param request:
    :return:
    """
    if request.method == 'GET':
        return render(request,'login.html')
    user = request.POST.get('user')
    pwd = request.POST.get('pwd')
    code = request.POST.get('code')
    if code.upper() != request.session['random_code'].upper():
        return render(request,'login.html',{'msg':'验证码错误'})

    if user == 'xiao' and pwd == '123':
        return redirect('http://www.py3study.com')

    return render(request, 'login.html', {'msg': '用户名或密码错误'})
View Code

和mange.py的同级路径,创建目录util,在里面创建文件code.py

import random
from PIL import Image,ImageDraw,ImageFont,ImageFilter
def check_code(width=120, height=30, char_length=5, font_file='kumo.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():
        """
        生成随机字母
        :return:
        """
        return chr(random.randint(65, 90))

    def rndColor():
        """
        生成随机颜色
        :return:
        """
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))

    # 写文字
    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())

    # 写干扰点
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

    # 写干扰圆圈
    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())

    # 画干扰线
    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)
View Code

在tempaltes目录创建login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="post">
        {% csrf_token %}
        <p>
            <input type="text" name="user" placeholder="用户名" />
        </p>
        <p>
            <input type="password" name="pwd" placeholder="密码" />
        </p>
        <p>
            <input type="text" name="code" placeholder="验证" />
            <img onclick="changeImg(this);" src="/code/" alt="" title="点击更换">
        </p>
        <input type="submit" value="提交">{{ msg }}
    </form>
    <script>
        function changeImg(ths) {
            ths.src = ths.src + '?';
        }
    </script>
</body>
</html>
View Code

将字体文件kumo.ttf放到和mange.py同级目录

下载链接如下:

链接:https://pan.baidu.com/s/1Ap0T3UitrWhcNp71WxeFJA 密码:v3xg

访问登录页面

输入一个错的,提示错误

输入一个正确的

跳转页面

今日作业:

组长创建一个项目,创建一个组,将组成员添加一一添加。

将各自写的博客系统代码提交到个人分支!登录页面,必须增加验证码功能!

使用的git命令如下:

生成ssh秘钥,一路狂回车
ssh-keygen

声明身份
git config --global user.name "肖祥"
git config --global user.email "[email protected]"

克隆代码
git clone [email protected]:Money-set/s11_menoy.git
创建个人分支
git branch xiaoxiang
切换到个人分支
git checkout xiaoxiang
复制代码到此目录
添加到暂存区
git add .
提交文件
git commit -m "xiao的博客系统"

提交到远程分支
git push origin master

猜你喜欢

转载自www.cnblogs.com/xiao987334176/p/9326290.html