LDAP for Django

DateReadtime 2 minutes Tags


This document will include the author's entire notes on installing and configuring ldap for django.


  1. Create a directory in /opt:

    mkdir /opt/that
  2. Create a virtualenv in that directory:

    virtualenv example

Make sure that you also source the directory before running pip commands. i.e.:

source /opt/that/example/bin/activate
  1. Install django:

    pip install django
  2. Install python-ldap

Here you have two options. Either install with pip or install using the package manager.

  1. Installing with package-manager:

    sudo apt-get install python-ldap
  2. Install with pip

First install dependencies:

sudo apt-get install libldap2-dev
sudo apt-get install libsasl2-dev

Then you should be able to install with pip:

pip install python-ldap

You may also have to install python-dev, if you haven't already

Dependency sources:

  1. Install django-auth-ldap

Here again you have two options. Either install with pip of install using your package manager.

  1. Installing with package-manager:

    sudo apt-get install python-django-auth-ldap

You may also want the docs package:

sudo apt-get install python-django-auth-ldap-doc
  1. Install with pip:

    pip install django-auth-ldap

LDAP Debugging

If you are unhappy with the quantity of logs produced by the ldap module patch the file "venv0/lib/python2.7/site-packages/ldap/ldapobject.py," in my expierence logging before and after line 100 works magically.

For example changing:

result = func(*args,**kwargs)


import logging
logger = logging.getLogger('django_auth_ldap')
logger.info("in  Func: %s Args: %s, Kw: %s" % (str(func), str(args), str(kwargs)))
result = func(*args,**kwargs)
logger.info("out Func: %s Result: %s" % (str(func), str(result)))

Please remember to see logs generated by the previous lines in django, you must also set up logging for "django_auth_ldap" in the settings file.

Active directory Error 49 Codes

Quick Guide to Error code 49 on AD systems

525user not found
52einvalid credentials
530not permitted to logon at this time
531not permitted to logon at this workstation
532password expired
533account disabled
701account expired
773user must reset password
775user account locked