Python Django - 配置不当:模块“django.contrib.auth.middleware”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25879725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django - ImproperlyConfigured: Module "django.contrib.auth.middleware"
提问by Jason Rueckert
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:
我正在运行一个 virtualenv 来尝试学习 Django,但无论出于何种原因,在安装 Django 后以及当我尝试访问默认的 Django 起始页时,我在浏览器中收到以下错误:
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
ImproperlyConfigured:模块“django.contrib.auth.middleware”没有定义“SessionAuthenticationMiddleware”属性/类
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.
如果有人对我为什么在 virtualenv 中收到此错误有任何见解,我将不胜感激。不过,我可以让服务器在非虚拟环境设置中正确运行。
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
采纳答案by Shubham Badal
easy solution
简单的解决方案
just remove
只需删除
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
from
从
MIDDLEWARE_CLASSES = (
...
...
)
in your project's settings.py
在您项目的settings.py 中
then, it should work!
那么,它应该工作!
回答by ZZY
Refer to the doc, the Django in your active virtualenv must be Django 1.7. And:
请参阅文档,您的活动 virtualenv 中的 Django 必须是 Django 1.7。和:
This middleware must appear after django.contrib.auth.middleware.AuthenticationMiddleware in MIDDLEWARE_CLASSES
这个中间件必须出现在 MIDDLEWARE_CLASSES 中的 django.contrib.auth.middleware.AuthenticationMiddleware 之后
Does it solve your issue?
它解决了你的问题吗?
回答by Jason Rueckert
So, I just ran a fresh install of my virtualenv and started the server, and now it's working as expected. Problem solved.
所以,我刚刚运行了我的 virtualenv 的全新安装并启动了服务器,现在它按预期工作。问题解决了。
回答by K.Karamazen
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 1.7 创建了一个 Django 项目,并尝试用 Django 1.6.8 运行它。我得到了同样的错误。我刚刚删除
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
from
从
MIDDLEWARE_CLASSES =
MIDDLEWARE_CLASSES =
in my project's settings.py. And it works.
在我项目的 settings.py 中。它有效。
回答by Michael Romrell
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/activateThen run: python manage.py runserver
我遇到了同样的错误。但是我忘记在运行我的服务器之前进入我的 VirtualEnv。因此,请确保从终端首先激活 virtualenv:source env/bin/activate然后运行:python manage.py runserver
回答by Cameron Carranza
I encountered an issue similar to this today (Using OS X Mavericks).
我今天遇到了与此类似的问题(使用 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.
首先,您的基本操作系统是否安装了 django?在使用 Virtualenv 之前,我直接在操作系统上运行 Django。我注意到的主要内容是我通常输入 django-admin,而不是 django-admin.py。这是在 1.7.1 版的 virtualenv 之外调用 Django。
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.
在我的 virtualenv 之外,我运行了pip uninstall django,然后又回到了我的 Virtualenv。最后运行没有 .py 的 django-admin --version 返回“zsh: command not found: django-admin”(它曾经返回 1.7.1),但是,运行 django-admin.py --version 返回 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
检查以确保您没有意外地从您的操作系统中直接引入外部版本的 django,您可能需要输入 django-admin.py
回答by Niilos
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
也可以编辑 manage.py 替换的第一行
\#!/usr/bin/env python
by
经过
\#!/usr/bin/env python3
and then run ./manage.py runserver
然后运行 ./manage.py runserver
(it seem to work, I don't know if it's authorized by django's project)
(好像可以用,不知道django的项目有没有授权)
回答by John Evans
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.
确保您从项目的根目录运行 $ source bin/activate 。否则,只需继续清除您的项目并创建一个新项目。如果你想成为一名 django 开发人员,请准备好制作大量的 virtualenv。
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.
例如,在单独的分支上工作时,有时更容易拥有两个不同的 virtualenv 等,当您移动到服务器时,您可能也会从 virtualenv 运行。因此,擅长制作它们并完成这些步骤是一个好主意。
It's easy to copy files between directories with the $ cp command.
使用 $cp 命令在目录之间复制文件很容易。
回答by user1776955
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
看起来您使用的是 1.7 版之前的 django 版本(具体来说是 1.6.4),并且直到 django 1.7 才引入了 SessionAuthenticationMiddleware。因此错误
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.
在右下角,您可以选择 django 的版本。选择适当的版本,然后按照您使用的 django 版本特定的教程进行操作。
回答by chao dai
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
我运行的是 Windows7/64 版本,有同样的错误。同意user1776955,指出Django的版本问题。所以最简单的方法是在 shell 中指向 Django-admin.py。就我而言,它是: python env\scripts\django-admin.py startproject my_django15_project

