老师报错了
老师运行报错,而且verify.py总有报错提醒---附图片
C:\envs\django\Scripts\python.exe C:/Users/Administrator/Desktop/py_learn/django_mall/utils/verify.py
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/py_learn/django_mall/utils/verify.py", line 110, in <module>
client.gen_code()
File "C:/Users/Administrator/Desktop/py_learn/django_mall/utils/verify.py", line 48, in gen_code
self.dj_request.session[self.session_key] = code
AttributeError: 'NoneType' object has no attribute 'session'
Process finished with exit code 1
"""
生成验证码:
1. 准备素材
字体(ttf),文字内容,颜色,干扰线
2. 画验证码
pip install Pillow 、random
创建图片
记录文字内容,django session【服务器,python代码】
abcdefg cookie 【浏览器】
(1) 第一次请求,cookie + session 对应关系生成
(2) 第二次请求,携带了cookie,找到对应的session【提交表单】
请求带上验证码参数 与 session中的验证码进行比较
3. io文件流
BytesIO
"""
import random
import os
from PIL import Image, ImageDraw, ImageFont
from django.conf import settings
from io import BytesIO
from django.http import HttpResponse
class VerifyCode(object):
""" 验证码类 """
def __init__(self, dj_request):
self.dj_request = dj_request
# 验证码长度
self.code_len = 4
# 验证码图片尺寸
self.img_width = 100
self.img_height = 30
# django中session的名称
self.session_key = 'verify_code'
def gen_code(self):
""" 生成验证码 """
# 1. 使用随机数生成验证码字符串
code = self._get_vcode()
# 2. 把验证码存在的session
self.dj_request.session[self.session_key] = code
# 3. 准备随机元素(背景颜色、验证码文字的颜色、干扰线、)
font_color = ['black', 'darkblue', 'darkred', 'brown', 'green', 'darkmagenta', 'cyan', 'darkcyan']
# RGB随机背景色
bg_color = (random.randrange(230, 255), random.randrange(230, 255), random.randrange(230, 255))
# 字体路径
font_path = os.path.join(settings.BASE_DIR, 'static', 'fonts', 'timesbi.ttf')
# 创建图片
im = Image.new('RGB', (self.img_width, self.img_height), bg_color)
draw = ImageDraw.Draw(im)
# 画干扰线
# 随机条数,到底画几条
for i in range(random.randrange(1, int(self.code_len / 2) + 1)):
# 线条的颜色
line_color = random.choice(font_color)
# 线条的位置
point = (
random.randrange(0, self.img_width * 0.2),
random.randrange(0, self.img_height),
random.randrange(self.img_width - self.img_width * 0.2, self.img_width),
random.randrange(0, self.img_height))
# 线条的宽度
width = random.randrange(1, 4)
draw.line(point, fill=line_color, width=width)
# 画验证码
for index, char in enumerate(code):
code_color = random.choice(font_color)
# 指定字体
font_size = random.randrange(15, 25)
font = ImageFont.truetype(font_path, font_size)
point = (index * self.img_width / self.code_len,
random.randrange(0, self.img_height / 3))
draw.text(point, char, font=font, fill=code_color)
buf = BytesIO()
im.save(buf, 'gif')
return HttpResponse(buf.getvalue(), 'image/gif')
def _get_vcode(self):
random_str = 'ABCDEFGHIGKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'
code_list = random.sample(list(random_str), self.code_len)
code = ''.join(code_list)
return code
def validate_code(self, code):
""" 验证验证码是否正确 """
# 1. 转变大小写
code = str(code).lower()
vcode = self.dj_request.session.get(self.session_key, '')
# if vcode.lower() == code:
# return True
# return False
return vcode.lower() == code
if __name__ == '__main__':
client = VerifyCode(None)
client.gen_code()
正在回答 回答被采纳积分+1
同学你好:
根据同学所提供的代码,没有发现错误
错误提示'NoneType' object has no attribute 'session' 空类型没有session属性,同学可以在
File "C:/Users/Administrator/Desktop/py_learn/django_mall/utils/verify.py", line 48, in gen_code
self.dj_request.session[self.session_key] = code
这里的时候,打印一下self.dj_request.session[self.session_key] 看一下是否有值,打印一下code是否有值,如果没有值追查一下调用VerifyCode类的地方,传入的参数是否正确。同学也可对比一下老师的views中的实现用户登陆的代码。
如果我的回答解决了同学的疑惑,欢迎采纳,祝同学学习愉快!
- 参与学习 人
- 提交作业 218 份
- 解答问题 3562 个
本阶段带你用Python开发一个网站,学习主流框架Django+Flask是Python Web开发的第一步,在基础知识上实现积分商城的项目开发,体验真实的项目开发流程,提高解决编程问题和效率的能力。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星