利用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





登录





html,body { margin:0; padding:0; overflow:hidden; height:100%; }
#jz-login { margin:0 auto; border:1px solid #666; background-color:#CCC; width:300px; }


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;



    {% if password_is_wrong %}

        ×
        

错误!

        用户名或密码错误
    {% endif %}         

用户登录

                     {% csrf_token %}                 {{ form }}