第六十七天 BBS之三

tuq2791 / 2024-03-01 / 原文

昨日内容回顾

-链式调用:
写一个类,类中的方法,每次都返回对象本身
class Person:
	def change_name(self,name):
		self.name=name
		return self
	def change_age(self,age):
		self.age=age
		return self

person.change_name(lqz)
person.change_age(19)

person.change_name(lqz).change_age(19)

一、登录页面搭建

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% load static %}
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    <script src="/static/jquery-3.3.1/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h1 class="text-center">登录功能</h1>
            <form action="" id="id_form">
                {% csrf_token %}
                <div class="form-group">
                    <label for="id_username">用户名</label>
                    <input type="text" id="id_username" name="username" class="form-control">
                </div>
                <div class="form-group">
                    <label for="id_password">密码</label>
                    <input type="password" id="id_password" name="password" class="form-control">
                </div>
                <div class="form-group">
                    <label for="id_code">验证码</label>
                    <div class="row">
                        <div class="col-md-6">
                            <input type="text" id="id_code" name="code" class="form-control">
                        </div>
                        <div class="col-md-6">
                            <img src="/get_code/" alt="" width="500px" height="40px" id="id_img">
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>
"""
这里的唯一难点是<img src="/get_code/" alt="" width="500px" height="40px" id="id_img">
其中的src="/get_code/"能直接调用后端的get_code函数
"""

二、自定义图片验证码

首先我们要了解怎么从内存中读写文件即IO操作
from io import BytesIO, StringIO

# 进行二进制的读写
f = BytesIO()
f.write('中文'.encode('utf-8'))  # 6
print(f.getvalue())  # b'\xe4\xb8\xad\xe6\x96\x87'

# 进行字符串内容的读写
f = StringIO()
f.write('hello')  # 5 f.write('') # 1
f.write('world!')  # 6
print(f.getvalue())  # hello world!