Django之路 - 实现登录随机验证码
发布时间:2020-12-17 00:07:13 所属栏目:Python 来源:网络整理
导读:登录验证码是每个网站登录时的基本标配,网上也有很多相应的文章, 但是从生成验证码到 应用到自己的网站上的全步骤,并没有看到很多, 为了节约大家的时间,我把整体步骤写下来, 即拿即用哈 ?1. 生成随机验证码 PIL font_path = number = 4 size = (100,30
登录验证码是每个网站登录时的基本标配,网上也有很多相应的文章, 但是从生成验证码到 应用到自己的网站上的全步骤,并没有看到很多, 为了节约大家的时间,我把整体步骤写下来, 即拿即用哈 ?1. 生成随机验证码
PIL
font_path =
number = 4
size = (100,30
bgcolor = (255,255,255
fontcolor = (0,255
linecolor = (255
draw_line =
line_number = (1,5
source = index range(0,10 .join(random.sample(source,number))
begin = end = draw.line([begin,end],fill =
width,height = size
image = Image.new(,(width,height),bgcolor)
font = ImageFont.truetype(font_path,25)
draw = ImageDraw.Draw(image)
text = gen_text()
font_width,font_height = draw.text(((width - font_width) / number,(height - font_height) / font= font,fill=fontcolor)
image = image.transform((width + 20,height +10),Image.AFFINE,(1,-0.3,-0.1,1,0),Image.BILINEAR)
image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)
image.save( %(save_path,filename))
(
== gene_code(,)
2. 如何应用到你的django项目中整个验证码的流程如下?
问题就卡在第3步,你在第1步生成验证码并返回给用户后,由于一会用户还需要把这个验证码提交过来,你在后台就需要拿用户输入的和你之前生成 的验证码进行对比是否相等, 所以你必须在生成验证码的同时,把验证码存下来,存到哪? 必然是缓存,这样直接在存的同时加个超时时间 , 就可以限定验证码有效期了。? 那存入缓存时的key是设置成什么呢?为了保证验证码的安全,我采取了以下设计 3.代码实现login视图== datetime.date.today().strftime(= % =(
random_filename = .join(random.sample(string.ascii_lowercase,4=30
</span><span style="color: #0000ff;">if</span> request.method == <span style="color: #800000;">"</span><span style="color: #800000;">POST</span><span style="color: #800000;">"</span><span style="color: #000000;">:
username </span>= request.POST.get(<span style="color: #800000;">'</span><span style="color: #800000;">username</span><span style="color: #800000;">'</span><span style="color: #000000;">)
password </span>= request.POST.get(<span style="color: #800000;">'</span><span style="color: #800000;">password</span><span style="color: #800000;">'</span><span style="color: #000000;">)
_verify_code </span>= request.POST.get(<span style="color: #800000;">'</span><span style="color: #800000;">verify_code</span><span style="color: #800000;">'</span><span style="color: #000000;">)
_verify_code_key </span>= request.POST.get(<span style="color: #800000;">'</span><span style="color: #800000;">verify_code_key</span><span style="color: #800000;">'</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">verify_code_key:</span><span style="color: #800000;">"</span><span style="color: #000000;">,_verify_code_key)
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">verify_code:</span><span style="color: #800000;">"</span><span style="color: #000000;">,_verify_code)
</span><span style="color: #0000ff;">if</span> cache.get(_verify_code_key) ==<span style="color: #000000;"> _verify_code:
</span><span style="color: #0000ff;">print</span>(<span style="color: #800000;">"</span><span style="color: #800000;">code verification pass!</span><span style="color: #800000;">"</span><span style="color: #000000;">)
user </span>= authenticate(username=username,password=<span style="color: #000000;">password)
</span><span style="color: #0000ff;">if</span> user <span style="color: #0000ff;">is</span> <span style="color: #0000ff;">not</span><span style="color: #000000;"> None:
login(request,user)
request.session.set_expiry(</span>60*60<span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> HttpResponseRedirect(request.GET.get(<span style="color: #800000;">"</span><span style="color: #800000;">next</span><span style="color: #800000;">"</span>) <span style="color: #0000ff;">if</span> request.GET.get(<span style="color: #800000;">"</span><span style="color: #800000;">next</span><span style="color: #800000;">"</span>) <span style="color: #0000ff;">else</span> <span style="color: #800000;">"</span><span style="color: #800000;">/</span><span style="color: #800000;">"</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">else</span><span style="color: #000000;">:
err_msg[</span><span style="color: #800000;">"</span><span style="color: #800000;">error</span><span style="color: #800000;">"</span>] = <span style="color: #800000;">'</span><span style="color: #800000;">Wrong username or password!</span><span style="color: #800000;">'</span>
<span style="color: #0000ff;">else</span><span style="color: #000000;">:
err_msg[</span><span style="color: #800000;">'</span><span style="color: #800000;">error</span><span style="color: #800000;">'</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">验证码错误!</span><span style="color: #800000;">"</span>
<span style="color: #0000ff;">return</span> render(request,<span style="color: #800000;">'</span><span style="color: #800000;">login.html</span><span style="color: #800000;">'</span>,{<span style="color: #800000;">"</span><span style="color: #800000;">filename</span><span style="color: #800000;">"</span>:random_filename,<span style="color: #800000;">"</span><span style="color: #800000;">today_str</span><span style="color: #800000;">"</span>:today_str,<span style="color: #800000;">"</span><span style="color: #800000;">error</span><span style="color: #800000;">"</span>:err_msg})</pre>
template文件?{% block body %}
<div id="container" class="cls-container">
{% endblock %}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |