您现在的位置是: 网站首页 >Django Django
用户登录captcha库验证码和记住密码功能实现
admin2018年10月16日 11:37 【Django | 权限 | 认证 】 1147人已围观
# 用户登录captcha库验证码和记住密码功能实现 **主要是使用django-simple-captcha验证码库,以及利用session来控制是否记住密码** ## 验证帐密Form表单 ```python from django import forms from captcha.fields import CaptchaField class LoginForm(forms.Form): # 用户名和密码不能为空 username = forms.CharField(required=True) password = forms.CharField(required=True, min_length=5) captcha = CaptchaField() # 验证码表单,按照文档直接复制过来 ``` ## 模板表单 增加记住密码的复选框,如果后台`remember_me`接收的数据为`on`,则表明用户记住密码 ```html <form method="post" action="{% if next_url %}{% url 'usercenter:login' %}?next={{ next_url }}{% else %}{% url 'usercenter:login' %}{% endif %}" autocomplete="off"> <input name='username' value="{% if login_form.username.value %}{{ login_form.username.value }}{% endif %}" type="text" class="form-control" placeholder="用户名/邮箱"/> {% if login_form.errors.username %} <span class="help-block m-b-none"> {{ login_form.errors.username.as_text }}</span> {% endif %} <input name='password' type="password" class="form-control" placeholder="密码"/> {% if login_form.errors.password %} <span class="help-block m-b-none"> {{ login_form.errors.password.as_text }}</span> {% endif %} {# login_form #} <div class="input-group"> <input type="hidden" name="captcha_0" value="{{ hashkey }}" required="" id="id_captcha_0"> <input type="text" name="captcha_1" class="form-control" style="width: 70%" placeholder="验证码" required="" id="id_captcha_1" autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false"> <span title="点击切换验证码" style="width: 30%; float: left;margin-top: 16px" id='captcha-refresh'> <img src="{{ image_url }}" id="captcha" style="height: 28px; width: 80px"> </span> </div> {% if login_form.errors.captcha %} <span class="help-block m-b-none"> {{ login_form.errors.captcha.as_text }}</span> {% endif %} <!-- <div class="input-group"> <input type="text" name="verify" class="form-control" style="width: 60%" placeholder="验证码"> <span title="点击切换验证码" id="id_change_verify" onclick="change_verify()" style="width: 40%; float: left;margin-top: 16px"> <img src="{% url 'usercenter:verify_image' 100 28 %}"/> </span> </div> --> <div class="checkbox"> <label> <input type="checkbox" name="remember_me" title="勾选记住密码,可在下次登录时直接进去系统">记住密码 </label> </div> {% if msg %} <br> <div class="alert alert-danger" style="padding: 5px;"> {{ msg }} </div> {% endif %} {% csrf_token %} <button type="submit" class="btn btn-success btn-block">登 录</button> <div class="row"> <div class="col-md-6"> <a type="button" class="btn btn-info btn-block" href="{% url 'usercenter:forget_password' %}">忘记密码</a> </div> <div class="col-md-6"> <a type="button" class="btn btn-primary btn-block" href="{% url 'usercenter:register' %}">用户注册</a> </div> </div> </form> ``` ## 登录视图逻辑 检测`request.session.get('remember_me')`是否为`True`,如果是则直接调味以前的页面而不是登录表单。 当用户登录时,获取`request.POST.get('remember_me')`是否为`on`,如果不是`on`,则配置`request.session['remember_me'] = False`,且将session过去时间设置为0,也就是浏览器关闭就删除session,`request.session.set_expiry(0)`;如果是`on`,则用户选择记住密码,将设置`request.session['remember_me'] = True`,这个字段用于登录检测。 ```python # 基于类的视图实现登录 class LoginView(View): def get(self, request): next_url = request.GET.get('next', '') # 访问登录,首先获取next的值,如果没有,则next_url赋值为'' # 检测是否勾选过记住密码,勾选过则为True if request.session.get('remember_me'): if next_url and next_url.strip() != '': # 如果下一跳地址不空,且不为空字符串,则登录成功跳回登录前的页面 return HttpResponseRedirect(next_url) else: return HttpResponseRedirect(reverse('blog:p_blog_index')) login_form = LoginForm() # 图片验证码,如果不分离出来,需要重新编写这些字段的样式 # hashkey验证码生成的秘钥,image_url验证码的图片地址 hashkey = CaptchaStore.generate_key() image_url = captcha_image_url(hashkey) return render(request, 'login.html', locals()) def post(self, request): next_url = request.GET.get('next') login_form = LoginForm(request.POST) hashkey = CaptchaStore.generate_key() image_url = captcha_image_url(hashkey) if login_form.is_valid(): user_name = request.POST.get('username', '') pass_word = request.POST.get('password', '') # 使用验证码库,不自定义 # verify = request.POST.get('verify', '') # verify_session = request.session.get('verify', '') # if verify.lower() != verify_session.lower(): # msg = '验证码错误' # return render(request, 'login.html', locals()) user = authenticate(username=user_name, password=pass_word) # 认证成功返回user对象,失败返回null if user: login(request, user) # 登录用户 # 记住密码功能 remember_me = request.POST.get('remember_me') if remember_me != 'on': # 如果没勾选记住密码复选框 request.session['remember_me'] = False request.session.set_expiry(0) # 设置超时时间,秒计算,如果为0,则关闭浏览器就删除session else: request.session['remember_me'] = True if next_url and next_url.strip() != '': # 如果下一跳地址不空,且不为空字符串,则登录成功跳回登录前的页面 return HttpResponseRedirect(next_url) else: return HttpResponseRedirect(reverse('blog:p_blog_index')) else: msg = '用户名或密码错误!' return render(request, 'login.html', locals()) else: return render(request, 'login.html', locals()) ``` ![BLOG_20181016_113824_15](/media/blog/images/2018/10/BLOG_20181016_113824_15.png "博客图集BLOG_20181016_113824_15.png")
很赞哦! (0)
相关文章
文章交流
- emoji