Django – ImproperlyConfigured: Module "django.contrib.auth.middleware"
Question:
I’m running a virtualenv to try to learn Django, but for whatever reason after installing Django and when I try to access the default Django start page, I get the following error in the browser:
A server error occurred. Please contact the administrator.
In the terminal window where I am running the server says the following error:
ImproperlyConfigured: Module “django.contrib.auth.middleware” does not define a “SessionAuthenticationMiddleware” attribute/class
If anyone has any insight as to why I’m getting this error in the virtualenv, I’d appreciate it. I can get the server to run correctly in a non-virtualenv setup, though.
Here is the full stack trace:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/wsgi.py", line 187, in __call__
self.load_middleware()
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/base.py", line 45, in load_middleware
mw_class = import_by_path(middleware_path)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/utils/module_loading.py", line 31, in import_by_path
error_prefix, module_path, class_name))
ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class
[16/Sep/2014 22:44:30] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/wsgi.py", line 187, in __call__
self.load_middleware()
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/base.py", line 45, in load_middleware
mw_class = import_by_path(middleware_path)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/utils/module_loading.py", line 31, in import_by_path
error_prefix, module_path, class_name))
ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class
Answers:
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?
So, I just ran a fresh install of my virtualenv and started the server, and now it’s working as expected. Problem solved.
easy solution
just remove
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
from
MIDDLEWARE_CLASSES = (
...
...
)
in your project’s settings.py
then, it should work!
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.
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
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
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)
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.
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.
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 envscriptsdjango-admin.py startproject my_django15_project
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
.
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:
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.
-
I have the same error:
File "C:UsersDANI3Envsrecetariolibsite-packagesdjangoutilsmodule_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'
-
I corrected the error make this change:
'social_django.middleware.SocialAuthExceptionMiddleware',
I’m running a virtualenv to try to learn Django, but for whatever reason after installing Django and when I try to access the default Django start page, I get the following error in the browser:
A server error occurred. Please contact the administrator.
In the terminal window where I am running the server says the following error:
ImproperlyConfigured: Module “django.contrib.auth.middleware” does not define a “SessionAuthenticationMiddleware” attribute/class
If anyone has any insight as to why I’m getting this error in the virtualenv, I’d appreciate it. I can get the server to run correctly in a non-virtualenv setup, though.
Here is the full stack trace:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/wsgi.py", line 187, in __call__
self.load_middleware()
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/base.py", line 45, in load_middleware
mw_class = import_by_path(middleware_path)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/utils/module_loading.py", line 31, in import_by_path
error_prefix, module_path, class_name))
ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class
[16/Sep/2014 22:44:30] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/wsgi.py", line 187, in __call__
self.load_middleware()
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/core/handlers/base.py", line 45, in load_middleware
mw_class = import_by_path(middleware_path)
File "/Users/jruecke/Python/JSON/lib/python2.7/site-packages/Django-1.6.5-py2.7.egg/django/utils/module_loading.py", line 31, in import_by_path
error_prefix, module_path, class_name))
ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class
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?
So, I just ran a fresh install of my virtualenv and started the server, and now it’s working as expected. Problem solved.
easy solution
just remove
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
from
MIDDLEWARE_CLASSES = (
...
...
)
in your project’s settings.py
then, it should work!
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.
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
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
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)
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.
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.
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 envscriptsdjango-admin.py startproject my_django15_project
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
.
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:
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.
-
I have the same error:
File "C:UsersDANI3Envsrecetariolibsite-packagesdjangoutilsmodule_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'
-
I corrected the error make this change:
'social_django.middleware.SocialAuthExceptionMiddleware',