利用Django和装饰器做一个简单的修改密码页面

view视图代码:

from django.shortcuts import render,redirect
from django.http import HttpResponse
from PIL import Image, ImageDraw, ImageFont
from django.utils.six import BytesIO # Create your views here.
def login_required(view_func):
def wrapper(request,*view_args,**view_kwargs):
if request.session.has_key('islogin'):
return view_func(request,*view_args,**view_kwargs)
else:
return redirect('/login')
return wrapper def child(request): return render(request,'booktest/child.html') def html_escape(requet):
return render(requet,'booktest/html_escape.html',{
'content':'<h1>h1标签<h1>'
}) def login(request):
'''显示登录页面'''
# 判断用户是否登录
if request.session.has_key('islogin'):
# 用户已登录, 跳转到首页
return redirect('/change_pwd')
else:
# 用户未登录
# 获取cookie username
if 'username' in request.COOKIES:
# 获取记住的用户名
username = request.COOKIES['username']
else:
username = '' return render(request, 'booktest/login.html', {'username':username}) def login_check(request):
'''登录校验视图'''
# request.POST 保存的是post方式提交的参数 QueryDict
# request.GET 保存是get方式提交的参数
print(request.method)
# 1.获取提交的用户名和密码
username = request.POST.get('username')
password = request.POST.get('password')
remember = request.POST.get('remember')
# 2.进行登录的校验
# 实际开发:根据用户名和密码查找数据库
# 模拟: smart 123
vscode = request.POST.get('vscode')
verifycode = request.session.get('verifycode')
if verifycode != vscode :
return redirect('/login') if username == 'smart' and password == '123':
# 用户名密码正确,跳转到首页
response = redirect('/change_pwd') # 判断是否需要记住用户名
if remember == 'on':
# 设置cookie username,过期时间1周
response.set_cookie('username', username, max_age=7*24*3600) # 记住用户登录状态
# 只有session中有islogin,就认为用户已登录
request.session['islogin'] = True
request.session['username'] = username
# 返回应答
return response
else:
# 用户名或密码错误,跳转到登录页面
return redirect('/login') @login_required
def change_pwd(request):
return render(request,'booktest/change_pwd.html') @login_required
def change_pwd_action(request):
password = request.POST.get('password')
username = request.session.get('username')
return HttpResponse('你的用户名为:%s密码为%s'%(username,password)) def verify_code(request): # 引入随机函数模块
import random
# 定义变量,用于画面的背景色、宽、高
bgcolor = (random.randrange(20, 100), random.randrange(
20, 100), 255)
width = 100
height = 25
# 创建画面对象
im = Image.new('RGB', (width, height), bgcolor)
# 创建画笔对象
draw = ImageDraw.Draw(im)
# 调用画笔的point()函数绘制噪点
for i in range(0, 100):
xy = (random.randrange(0, width), random.randrange(0, height))
fill = (random.randrange(0, 255), 255, random.randrange(0, 255))
draw.point(xy, fill=fill)
# 定义验证码的备选值
str1 = 'ABCD123EFGHIJK456LMNOPQRS789TUVWXYZ0'
#随机选取4个值作为验证码
rand_str = ''
for i in range(0, 4):
rand_str += str1[random.randrange(0, len(str1))]
# 构造字体对象,ubuntu的字体路径为“/usr/share/fonts/truetype/freefont”
font = ImageFont.truetype('FreeMono.ttf', 23)
# 构造字体颜色
fontcolor = (255, random.randrange(0, 255), random.randrange(0, 255))
# 绘制4个字
draw.text((5, 2), rand_str[0], font=font, fill=fontcolor)
draw.text((25, 2), rand_str[1], font=font, fill=fontcolor)
draw.text((50, 2), rand_str[2], font=font, fill=fontcolor)
draw.text((75, 2), rand_str[3], font=font, fill=fontcolor)
# 释放画笔
del draw
# 存入session,用于做进一步验证
request.session['verifycode'] = rand_str
# 内存文件操作
buf = BytesIO()
# 将图片保存在内存中,文件类型为png
im.save(buf, 'png')
# 将内存中的图片数据返回给客户端,MIME类型为图片png
return HttpResponse(buf.getvalue(), 'image/png')

  

上一篇:JQuery源码解析(十一)


下一篇:通过Nginx实现一个简单的网站维护通知页面