# -*- coding: utf-8 -*-
from django.contrib.auth import authenticate,login as user_login,logout
from django.shortcuts import HttpResponseRedirect, render
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

@login_required
def Logout(request):
    logout(request)
    response = HttpResponseRedirect('/')
    return response


def login_view(request):

    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        if not username or not password:
            messages.info(request, u'用户名或密码不能为空！')
            print 222
            return render(request, 'login.html', locals())

        user = authenticate(username = username,password = password)
        if user is not None and user.is_active:

            user_login(request,user)
            return HttpResponseRedirect('/')
        else:
            messages.info(request, u'用户名或密码错误！')
            print 333
            return render(request, 'login.html',locals())
    else:
        # user = User.objects.create_user(username='yun',
        #                                 password='111')
        # user.save()
        return render(request, 'login.html')

