djangoユーザー認証


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 PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



  






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 }}