利用django自帶認證功能實現用戶登錄認證。
views.py
# Create your views here. from django.shortcuts import render_to_response,render,get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth.decorators import login_required from django.template.context import RequestContext from django.contrib.auth.models import User from django.contrib import auth from forms import LoginForm def login(request): if request.method == 'GET': form = LoginForm() return render_to_response('login.html',RequestContext(request,{'form':form,})) else: form = LoginForm(request.POST) if form.is_valid(): username = request.POST.get('username','') password = request.POST.get('password','') user = auth.authenticate(username=username,password=password) if user is not None and user.is_active: auth.login(request,user) return render_to_response('index.html',RequestContext(request)) else: return render_to_response('login.html',RequestContext(request,{'form':form,'password_is_wrong':True})) else: return render_to_response('login.html',RequestContext(request,{'form':form,})) @login_required def logout(request): auth.logout(request) return HttpResponseRedirect("/login/") @login_required def index(request): return render_to_response('index.html')
froms.py
#coding=utf-8 from django import forms from django.contrib.auth.models import User class LoginForm(forms.Form): username = forms.CharField( required = True, label="用戶名", error_messages={'required':'請輸入用戶名'}, widget=forms.TextInput( attrs={ 'placeholder': "用戶名", 'class':'form-control' } ) ) password = forms.CharField( required=True, label="密碼", error_messages={'required':'請輸入密碼'}, widget=forms.PasswordInput( attrs={ 'placeholder':"密碼", 'class':'form-control' } ), ) def clean(self): if not self.is_valid(): raise forms.ValidationError("用戶名和密碼爲必填項") else: cleaned_data = super(LoginForm,self).clean()
login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>登錄</title> <script type="text/javascript" src="/static/bootstrap/js/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap-theme.min.css"> <script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script> <style type="text/css"> html,body { margin:0; padding:0; overflow:hidden; height:100%; } #jz-login { margin:0 auto; border:1px solid #666; background-color:#CCC; width:300px; } </style> <script type="text/javascript"> function makeItMiddle() { document.getElementById('jz-login').style.marginTop = (document.getElementsByTagName('body')[0].offsetHeight - document.getElementById('jz-login').offsetHeight) / 2 + 'px'; } window.onload = makeItMiddle; window.onresize = makeItMiddle; </script> </head> <body> {% if password_is_wrong %} <div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button> <h4>錯誤!</h4> 用戶名或密碼錯誤 </div> {% endif %} <div class="well" id="jz-login" style="margin:auto"> <h1>用戶登錄</h1> <form class="form-horizontal" action="" method="post"> {% csrf_token %} {{ form }} <p> </p> <p class="form-actions"> <input type="submit" value="登錄" class="btn btn-primary"> <a href="/contactme/"><input type="button" value="忘記密碼" class="btn btn-danger"></a> <a href="/contactme/"><input type="button" value="新員工?" class="btn btn-success"></a> </p> </form> </div> <script src="/static/bootstrap/js/jquery.min.js"></script> <script src="/static/bootstrap/js/bootstrap.min.js"></script> </body> </html>