#生成随机验证码
from PIL importImagefrom io importBytesIOfrom PIL importImageDraw,ImageFontdefcheck_code(request):##Django imag标签src属性导入图片的原理
#f=open('static/imgs/1.jpg','rb') #注意:static/imgs/1.jpg不能写成 /static/imgs/1.jpg
#data=f.read()
#f.close()
#return HttpResponse(data)
#创建code.png写入到本地BBS目录下
#from PIL import Image
#img=Image.new(mode='RGB',size=(120,30),color=(255,255,255)) #默认写入白板,随后在白板上写入字符串等
#f=open('code.png','wb')
#img.save(f,'png') #png为图片后缀
#f.close()
##将本地图片code.png读取到内存并使网页图片能显示出来
#f=open('code.png','rb')
#data=f.read()
#f.close()
#return HttpResponse(data)
#以上操作比较麻烦,需要写入本地又读取到内存中
#以下操作为在内存中开辟空间,在内存中进行读写等操作
#from io import BytesIO
##写入内存中
#f=BytesIO()
#img=Image.new(mode='RGB',size=(120,30),color=(255,255,255))
#img.save(f,'png')
##从内存中读出来
#data=f.getvalue()
#return HttpResponse(data)
#from io import BytesIO
#from PIL import ImageDraw,ImageFont
f=BytesIO()
img=Image.new(mode='RGB',size=(120,30),color=(255,255,255))
draw=ImageDraw.Draw(img,mode='RGB')#draw.point([100, 25], fill="red")
#draw.point([30, 20], fill=(187, 255, 255))
#draw.line((10, 10, 20, 30), fill='red')
#draw.line((100, 10, 30, 20), fill=(180, 255, 255))
#draw.arc((30, 25, 80, 10), 0, 360, fill="red")
#font = ImageFont.truetype("kumo.ttf", 28)
#draw.text([0, 0], 'python', "red",font=font)
importrandom#char_list=[]
#for i in range(5):
#char=chr(random.randint(65,109))
#char_list.append(char)
#''.join(char_list)
##列表生成式,但是字母随机了,字体颜色不随机,所以不用列表生成式
#v=''.join([ chr(random.randint(65,109)) for i in range(5)])
#for i in range(5):
#char=chr(random.randint(65,109))
#font=ImageFont.truetype("kumo.ttf", 28)
#draw.text([i*24,0],char,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),font=font)
#写入的字符串需要自己知道内容,方便表单的校验
#char_list = []
#for i in range(5):
#char=chr(random.randint(65,109))
#char_list.append(char)
#font=ImageFont.truetype("kumo.ttf", 28)
#draw.text([i*24,0],char,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),font=font)
#code=''.join(char_list)
#但又面临的问题是POST请求时提交数据,获取不到,所以需要写入session里,并进入login函数中增加POST请求
#char_list = []
#for i in range(5):
#char = chr(random.randint(65, 109))
#char_list.append(char)
#font = ImageFont.truetype("kumo.ttf", 28)
#draw.text([i * 24, 0], char, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
#font=font)
#img.save(f,'png')
#data=f.getvalue()
#code = ''.join(char_list)
#request.session['code'] = code
#return HttpResponse(data)
#以上验证码代码太长,将内容封装到BBS目录下utils目录中
from utils.random_check_code importrandom_check_code
img,code=random_check_code()
stream=BytesIO()
img.save(stream,'png')
request.session['code']=codereturnHttpResponse(stream.getvalue())#登录页面,使用Form表单#生成Form表单
from django.forms importFormfrom django.forms importwidgetsfrom django.forms importfieldsclassLoginForm(Form):
user=fields.CharField(
widget=widgets.TextInput(attrs={'class':"form-control",'placeholder':'用户名'}),
label="用户名",
required=True,
max_length=8,
min_length=2,
error_messages={'required':'用户名不能为空','min_length':'用户名不得少于2个字符','max_length':'用户名不得多于8个'}
)
pwd=fields.CharField(
widget=widgets.TextInput(attrs={'class':"form-control",'placeholder':'密码','type':'password'}),
label="密码",
required=True,
max_length=10,
min_length=2,
error_messages={'required': '密码不能为空','min_length': '密码不得少于2个字符','max_length': '密码不得多于10个'}
)
code=fields.CharField(
widget=widgets.TextInput(attrs={'class':"form-control",'placeholder':'验证码'}),
label="验证码",
required=True,
error_messages={'required':'验证码不能为空'}
)deflogin_form(request):if request.method=='GET':
obj=LoginForm()return render(request, 'login_form.html', {'obj': obj})else:
obj=LoginForm(request.POST)ifobj.is_valid():
input_user=obj.cleaned_data['user']
input_pwd=obj.cleaned_data['pwd']
input_code=obj.cleaned_data['code']
session_code=request.session.get('code')if models.UserInfo.objects.filter(username=input_user,password=input_pwd).first():if input_code.upper()==session_code.upper():return render(request,'information.html')else:return render(request, 'login_form.html', {'obj': obj, 'msg': '验证码错误'})return render(request, 'login_form.html',{'obj':obj,'msg':'用户名或密码错误'})else:
v=obj.errorsprint(v)return render(request,'login_form.html',{'obj':obj})
views