[Django]-Django – ImproperlyConfigured: Module "django.contrib.auth.middleware"

39👍

✅

easy solution

just remove

'django.contrib.auth.middleware.SessionAuthenticationMiddleware',

from

MIDDLEWARE_CLASSES = (
...
...
)

in your project’s settings.py

then, it should work!

18👍

I was getting the same error. But i had forgotten to get into my VirtualEnv BEFORE running my server.
So make sure that from terminal you first activate virtualenv: source env/bin/activate Then run: python manage.py runserver

14👍

As we can see in Django 1.8 release notes

django.contrib.auth.middleware.SessionAuthenticationMiddleware was
added in Django 1.7.

And you are using Django-1.6.5 in your virtual environment, hence the does not define error.

Probably you have a newer version of Django installed in your “normal” environment and the server runs correctly. To fix this upgrade Django version inside your virtual environment (prior to updating Django be sure to activate your virtual environment!)

Now to add my two cents to the answers, because everything until now was repetition from ZZY and user1776955

If you run pip install -U Django you will probably bump your version to something higher than 1.10 and then the following will apply:

In Django 1.10, session verification will be enabled regardless of
whether or not SessionAuthenticationMiddleware is enabled (at which
point SessionAuthenticationMiddleware will have no significance)

Hence it will be safe to delete it and if you update past 2.0 you will HAVE to delete it because Django 2.0 release notes state that:

The SessionAuthenticationMiddleware class is removed. It provided no
functionality since session authentication is unconditionally enabled in
Django 1.10.

Also a bit unrelated but relevant when updating to this version is that

Support for old-style middleware using settings.MIDDLEWARE_CLASSES is removed

As far as my experience goes it is enough to change MIDDLEWARE_CLASSES to just MIDDLEWARE and delete 'django.contrib.auth.middleware.SessionAuthenticationMiddleware' from the list.

5👍

Refer to the doc, the Django in your active virtualenv must be Django 1.7. And:

This middleware must appear after django.contrib.auth.middleware.AuthenticationMiddleware in MIDDLEWARE_CLASSES

Does it solve your issue?

4👍

run

python3 manage.py runserver

instead of

python manage.py runserver

or

./manage.py runserver

You can also edit the first line of manage.py replacing

\#!/usr/bin/env python

by

\#!/usr/bin/env python3

and then run ./manage.py runserver

(it seem to work, I don’t know if it’s authorized by django’s project)

2👍

I have created a Django project with Django 1.7 and tried to run it with Django 1.6.8. And I got the same error. I have just removed

‘django.contrib.auth.middleware.SessionAuthenticationMiddleware’,

from

MIDDLEWARE_CLASSES =

in my project’s settings.py.
And it works.

2👍

I encountered an issue similar to this today (Using OS X Mavericks).

First of all, do you have django installed to your base OS? Before using Virtualenv I was running Django directly on the OS. The main thing I noticed is I usually type django-admin, not django-admin.py. This was calling Django outside of the virtualenv, which was version 1.7.1.

Outside of my virtualenv I ran pip uninstall django, then went back into my Virtualenv. Running django-admin –version without a .py at the end returned “zsh: command not found: django-admin” (it used to return 1.7.1), however, running django-admin.py –version returned 1.6.5.

Check to make sure you aren’t accidentally bringing in an outside version of django directly from your OS, you may need to type django-admin.py

2👍

It looks like you are using a version of django prior to version 1.7 (1.6.4 to be specific), and SessionAuthenticationMiddleware was not introduced until django 1.7. Hence the error

Documentation can be found here
https://docs.djangoproject.com/en/1.7/ref/middleware/#django.contrib.auth.middleware.SessionAuthenticationMiddleware

On the bottom right, you can choose the version of django. Select the appropriate version, and follow the tutorial specific to the version of django you are using.

1👍

So, I just ran a fresh install of my virtualenv and started the server, and now it’s working as expected. Problem solved.

0👍

Make sure that you are running $ source bin/activate from the root of your project. Otherwise just go ahead and wipe out your project and make a new one. And if you want to be a django dev get ready to make lots of virtualenv’s.

When working on separate branches for example, it’s sometimes easier to have two different virtualenv’s, etc and when you move to the server you’ll probably be running from a virtualenv as well. So it’s a good idea to get good at making them and going through the steps.

It’s easy to copy files between directories with the $ cp command.

0👍

I am running Windows7/64 version, had the same error. Agreed with user1776955, who pointed out the version problem of Django.
so the easiest way to do is pointing to the Django-admin.py in shell. In my case, it is:
python env\scripts\django-admin.py startproject my_django15_project

0👍

Just run it in Python 3 instead:

python3 manage.py runserver

If that would work, consider also applying some app migrations via: python3 manage.py migrate.

-1👍

  1. I have the same error:

    File "C:\Users\DANI3\Envs\recetario\lib\site-packages\django\utils\module_loading.py", line 23, in import_string
      return getattr(module, class_name)
    django.core.exceptions.ImproperlyConfigured: WSGI application 'recetario.wsgi.application' could not be loaded; Error importing module: 'Module "social_django.middleware" does not define a "SocialAythExceptionMiddleware" attribute/class'
    
  2. I corrected the error make this change:

    'social_django.middleware.SocialAuthExceptionMiddleware',
    

Leave a comment