Exemplo n.º 1
0
def login(request):
    if request.user.is_authenticated():
        redirect_to = request.REQUEST.get('next') or '/'
        return HttpResponseRedirect(redirect_to)
        #return HttpResponseRedirect('/')
    if request.method == 'GET':
        c = {}
        c.update(csrf(request))
        c['form'] = LoginForm()
        return render_to_response('authentication/login.html', c)
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            data = form.clean()
            user = authenticate(username=data['username'],
                                password=data['password'])
            if user and user.is_active:
                auth.login(request, user)
                redirect_to = request.REQUEST.get('next') or '/'
                return HttpResponseRedirect(redirect_to)
            else:
                c = {}
                c.update(csrf(request))
                c['error'] = 'this username could not be authenticated.  Please ensure this user exists and has been activated before logging in'
                c['form'] = form
                return render_to_response('authentication/login.html', c)
        c = {}
        c.update(csrf(request))
        c['form'] = form
        c['error'] = 'the username and/or password is incorrect'
        return render_to_response('authentication/login.html', c)
    return HttpResponseRedirect('/')