1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| def myadmin_login(request): return render(request, 'myadmin/login.html')
def myadmin_dologin(request): if request.session.get('verifycode').upper() != request.POST.get('vcode').upper(): return HttpResponse('<script>alert("验证码错误");location.href="/myadmin/login/"</script>')
if request.POST['username'] == 'admin' and request.POST['password'] == '123456': request.session['AdminUser'] = {'username': 'admin', 'uid': 10} return HttpResponse('<script>alert("欢迎登录");location.href="/myadmin/"</script>')
return HttpResponse('<script>alert("账号或密码错误");location.href="/myadmin/login/"</script>')
def verifycode(request): from PIL import Image, ImageDraw, ImageFont 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) 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' rand_str = '' for i in range(0, 4): rand_str += str1[random.randrange(0, len(str1))] font = ImageFont.truetype('simhei.ttf', 23) fontcolor = (255, random.randrange(0, 255), random.randrange(0, 255)) 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 request.session['verifycode'] = rand_str import io buf = io.BytesIO() im.save(buf, 'png') return HttpResponse(buf.getvalue(), 'image/png')
|