上一篇中,我们已经打开了Django自带的用户认证模块,并配置了数据库连接,创建了相应的表,本篇我们将在Django自带的用户认证的基础上,实现自己个性化的用户登录和注销模块。
首先,我们自己定义一个用户登录表单(forms.py):
从django导入形式 从django.contrib.auth。模型导入用户 从bootstrap_toolkit。小部件进口BootstrapDateInput、BootstrapTextInput BootstrapUneditableInput 类LoginForm (forms.Form): 用户名=forms.CharField ( 要求=True, 标签=u”用户名”, error_message={“要求”:“请输入用户名"}, 小部件=forms.TextInput ( attrs={ “占位符”:u”用户名”, } ), ) 密码=forms.CharField ( 要求=True, 标签=u "密码”, error_message={“要求”:u '请输入密码"}, 小部件=forms.PasswordInput ( attrs={ “占位符”:u”密码”, } ), ) def清洁(自我): 如果不是self.is_valid (): 提高forms.ValidationError (u”用户名和密码为必填项”) 其他: cleaned_data=https://www.yisu.com/zixun/super (LoginForm,自我).clean ()
我们定义的用户登录表单有两个域用户名和密码,这两个域都为必填项。
接下来,我们定义用户登录视图(views.py),在该视图里实例化之前定义的用户登录表单
django。快捷方式进口render_to_response,渲染,get_object_or_404 django。http进口HttpResponse HttpResponseRedirect 从django.contrib.auth。模型导入用户 django。contrib进口认证 django。contrib导入信息 从django.template。上下文进口RequestContext 从django.forms。进口formset_factory形态层组 从django.core。paginator进口paginator PageNotAnInteger EmptyPage 从bootstrap_toolkit。小部件进口BootstrapUneditableInput 从django.contrib.auth.decorators进口login_required 从养成进口LoginForm def登录(请求): 如果请求。方法==玫健? 形式=LoginForm () 返回render_to_response(登录。html, RequestContext(请求,{“形式”:形式,})) 其他: 形式=LoginForm (request.POST) 如果form.is_valid (): 用户名=request.POST。(“用户名”,“) 密码=request.POST。(‘密码’,”) 用户身份验证。验证(用户名=用户名、密码=密码) 如果用户不是没有和user.is_active: 身份验证。登录(请求,用户) 返回render_to_response(指数。html, RequestContext(请求)) 其他: 返回render_to_response(登录。html, RequestContext(请求,{“形式”:形式,“password_is_wrong”:真正})) 其他: 返回render_to_response(登录。html, RequestContext(请求,{“形式”:形式,}))
该视图实例化了之前定义的LoginForm,它的主要业务逻辑是:
1。判断必填项用户名和密码是否为空,如果为空,提示”用户名和密码为必填项”的错误信息
2。判断用户名和密码是否正确,如果错误,提示”用户名或密码错误”的错误信息
3。登陆成功后,进入主页(index . html)
其中,登录页面的模板(login.html)定义如下:
& lt; !DOCTYPE html> {%负载bootstrap_toolkit %} {%从未来加载url %} & lt; html lang=癳n”比; & lt; head> & lt;元charset=皍tf - 8”比; & lt; title>数据库脚本发布系统& lt;/title> & lt;元名称=懊枋觥蹦谌?氨? & lt;元name=白髡摺蹦谌?爸煜越堋痹? {% bootstrap_stylesheet_tag %} {% bootstrap_stylesheet_tag“响应”%} & lt;风格类型=" text/css "比; 身体{ padding-top: 60 px; } & lt;/style> & lt; !——(如果lt IE 9)比; & lt;脚本src=" https://www.yisu.com//html5shim.googlecode.com/svn/trunk/html5.js "祝辞& lt;/script> & lt; ! [endif]——比; & lt;脚本src=" https://www.yisu.com//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js "祝辞& lt;/script> {% bootstrap_javascript_tag %} {%块extra_head %} {% endblock %} & lt;/head> & lt; body> {%如果password_is_wrong %} & lt; div class="警惕alert-error”比; & lt;按钮类型=鞍磁ァ眂lass=肮乇铡眃ata-dismiss=熬ā弊4恰? lt;/button> & lt; h5>错误! & lt;/h5>用户名或密码错误 & lt;/div> {% endif %} & lt; div类=班拧北? & lt; h2>数据库脚本发布系统& lt;/h2> & lt; p>& lt;/p> & lt;形式类=" form-horizontal " action=" method=" post "比; {% csrf_token %} {{形式| as_bootstrap:“横向”}} 祝辞& lt; p class="表单操作”; & lt;输入类型="提交" value=" https://www.yisu.com/zixun/登录”类=癰tn btn-primary”比; & lt; a href=" https://www.yisu.com/contactme/" rel=巴獠縩ofollow”rel=巴獠縩ofollow”祝辞& lt;输入类型=鞍磁ァ奔壑?" https://www.yisu.com/zixun/忘记密码”类=癰tn btn-danger祝辞& lt;/a> & lt; a href=" https://www.yisu.com/contactme/" rel=巴獠縩ofollow”rel=巴獠縩ofollow”祝辞& lt;输入类型=鞍磁ァ奔壑?" https://www.yisu.com/zixun/新员工?”class=癰tn btn-success”祝辞& lt;/a> & lt;/p> & lt;/form> & lt;/div> & lt;/body> & lt;/html>Django实战之用户认证(用户登录与注销)