django中间件是一个一个的类,应用中间件要在setting里找打MIDDLEWARE
这个地方写你写的中间件也就是类的路径
class M1(MiddlewareMixin):
'''中间件'''
def process_request(self, request):
print('M1.进来了')
def process_response(self, request, response):
print('M1走了')
return response
class M2(MiddlewareMixin):
'''中间件'''
def process_request(self, request):
print('M2.进来了')
def process_response(self, request, response):
print('M2走了')
return response
用的话就在setings.py middleware里写路径就可以
中间件实现登录校验
from django.http import HttpResponse
from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin
class AuthMiddleware(MiddlewareMixin):
"""中间件"""
def process_request(self, request):
# 0.排除那些不需要登录就能访问的页面
# request.path_info 获取当前请求的URL /login/
if request.path_info == '/login/':
return
# 1.读取当前访问用户的session信息,如果能读取到,说明已经登录过,就可以继续向后走
info_dict = request.session.get("info")
if info_dict:
return
# 2 如果没有登录过,重新回到登录页面
return redirect('/login/')
# 如果process_request没有返回值,继续向后走,如果有返回值HttpResponse就返回去
# 不再向下走
print('M1.进来了')
def process_response(self, request, response):
print('M1走了')
return response
注销
设置当前用户
图片验证码
一个知识点 生成动态的图片
from PIL import Image, ImageDraw, ImageFont
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255)) # 生成画布
draw = ImageDraw.Draw(img, mode='RGB') # 生成画笔
font = ImageFont.truetype('Monaco.ttf', 28) # 第一个参数表示字体文件路径,第二个表示字体大小
draw.text([0, 0], 'python', "red", font=font) # 0,0表示从什么位置开始写,python代表写的内容,red代表字体颜色
with open('code.png', 'wb') as f:
img.save(f, format='png')
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():
'''生成随机字母'''
return chr(random.randint(65, 90))
def rndColor():
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)
if __name__ == '__main__':
img, code_str = check_code()
print(code_str)
with open('code.png', 'wb') as f:
img.save(f, format='png')
生成的验证码与用户提交的验证码是否一致怎么校验
Ajax请求
**
浏览器向网站发送请求有get和post请求,目前都是通过url和表单的形式提交,这种提交都会重新刷新页面。
通过Ajax进行请求就会不刷新,偷偷的发请求,页面不会做任何刷新
依赖jQuery
编写ajax代码
用Ajax请求将input框的数据提交到后台
实例1
实例2
实例3
实例4