`
zlcjun
  • 浏览: 2426 次
社区版块
存档分类
最新评论

django1.4 自定义实现验证码

阅读更多

 

根据网上搜寻的,加上自己的理解,简单是实现了基于python2.7.5和django1.4的动态验证码

 

后台生成验证码的python代码写入views.py,

注意:导入PIL下的Image, ImageDraw, ImageFont,需安装PIL的插件,我使用的是PIL-1.1.7.win32-py2.7,附件中有

代码如下:

 

 #获取校验码,并返回给浏览器
def getCheckCodeImage(request):
	try:
		import Image
		import ImageDraw
		import ImageFont
	except ImportError:
		from PIL import Image, ImageDraw, ImageFont 
	#验证码的长度
	clength = 4
	#获取到验证码的值
	code_dict = initCheckCodeVal(clength)
	#取出列表类型的验证码值
	rand_list = code_dict['clist']
	#取出字符串类型的验证码值
	rand_str = code_dict['cstr']
	#设置字体,需设置的字体包simsun.ttc与python源码同级目录

	font = ImageFont.truetype('simsun.ttc',random.randint(18,25))
	#Image背景颜色
	bg_color = (255,255,255)
	#Image的长和宽
	i_width,i_height = clength*20,30
	#初始化Image对象
	im = Image.new('RGB',(i_width,i_height),bg_color)
	draw = ImageDraw.Draw(im)
	for i in range(0,clength):
		in_x = 10+i*10+random.randint(1,7)
		in_y = random.randint(2,15)
		draw.text((in_x,5), rand_list[i],font=font,fill=(0,0,0))
	#随机设置干扰线
	for i in range(0,3):
		linecolor = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
		#都是随机的
		x1 = random.randint(0,10)
		x2 = random.randint(i_width-10,i_width)
		y1 = random.randint(5,i_height-5)
		y2 = random.randint(5,i_height-5)
		draw.line([(x1, y1), (x2, y2)], linecolor)
	del draw
	#将验证码转换成小写的,并保存到session中
	request.session['checkcode'] = rand_str.lower()
	buf = StringIO.StringIO()
	#将image信息保存到StringIO流中
	im.save(buf, 'gif')
	return HttpResponse(buf.getvalue(),'image/gif')
#获取验证码的值
def initCheckCodeVal(length=4):
	codes = ['0','2','3','4','5','6','7','8','9',
	'a','b','c','d','e','f','g','h','i','j',
	'k','m','n','o','p','q','r','s','t','u',
	'v','w','x','y','z','A','B','C','D','E',
	'F','G','H','I','J','K','L','M','N','O',
	'P','Q','R','S','T','U','V','W','X','Y',
	'Z']
	code_list = []
	code_str = ''

	for i in range(0,length):
		temp = codes[random.randint(0,59)]
		code_list.append(temp)
		code_str+=temp
	return {'clist':code_list,'cstr':code_str}

获取验证码url路径配置,urls.py

 

url(r'^msg/checkCodeImage/$', 'mysite.views.getCheckCodeImage',name='check code')

 网页上使用img标签加载图片

 

 

<img class="codeimg" onclick="this.setAttribute('src','/msg/checkCodeImage?nocache='+Math.random());" src="/msg/checkCodeImage"/>

 页面效果如下图:

 

 

本人初学python和django,如有错误地方望高手多多指点

 

 

 

 

  • 大小: 775 Bytes
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics