Python Django:没有名为 security 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25901481/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:48:24  来源:igfitidea点击:

Python Django : No module named security

pythondjangomodulevirtualenv

提问by Zat42

When I deploy my project on an Ubuntu Server, using a virtualenv, I got this error :

当我使用 virtualenv 在 Ubuntu 服务器上部署我的项目时,出现此错误:

[17/Sep/2014 22:29:00] "GET / HTTP/1.1" 500 59
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/zat42/myproject/monarch/env/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 64, in __call__
return self.application(environ, start_response)
File "/home/zat42/myproject/monarch/env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 168, in __call__
self.load_middleware()
File "/home/zat42/myproject/monarch/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 44, in load_middleware
mw_class = import_string(middleware_path)
File "/home/zat42/myproject/monarch/env/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 26, in import_string
module = import_module(module_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named security

I don't know why there is this error my configuration works fine with a fresh install... But when I copy my current project, I got Error 500. I tried to deploy "part after part" but I can't find what's wrong.

我不知道为什么会出现这个错误,我的配置在全新安装时可以正常工作......但是当我复制当前项目时,我收到了错误 500。我尝试部署“一部分又一部分”,但我找不到是什么错误的。

Tell me if you need more of my files because I don't really know where is the problem...

告诉我你是否需要更多我的文件,因为我真的不知道问题出在哪里......

Thank you.

谢谢你。

采纳答案by Fox1978

I met the same problem. Finnaly, I found I'm using django 1.7.1 to run a 1.8dev generated project. When I switch back to 1.7.1, and remove ‘django.middleware.security.SecurityMiddleware' in setting.py, it seems ok.

我遇到了同样的问题。最后,我发现我正在使用 django 1.7.1 来运行 1.8dev 生成的项目。当我切换回 1.7.1,并在 setting.py 中删除 'django.middleware.security.SecurityMiddleware' 时,似乎没问题。

回答by Zat42

So, I found a solution :

所以,我找到了一个解决方案:

'django.middleware.security.SecurityMiddleware'

This line is in MIDDLEWARE_CLASSES, from settings.py. When I delete this line I have no more problems with the security module but I guess this is not a good way to solve the problem ... I guess this line is in relation with the crsf token and things like that.

该行位于 settings.py 中的 MIDDLEWARE_CLASSES 中。当我删除这一行时,安全模块不再有任何问题,但我想这不是解决问题的好方法......我猜这行与 crsf 令牌和类似的东西有关。

Any other idea to fix the problem ?

还有其他解决问题的想法吗?

回答by iross

I ran into this same problem. It turned out that I was inadvertently using my machine's version of django-admin.py to start my Django project, rather than the one installed within the virtualenv. I ended up having to source bin/activateagain after installing django within the virtualenv, before running any of the django-admin commands.

我遇到了同样的问题。结果是我无意中使用了我机器的 django-admin.py 版本来启动我的 Django 项目,而不是安装在 virtualenv 中的那个。source bin/activate在运行任何 django-admin 命令之前,我最终不得不在 virtualenv 中安装 django 之后再次这样做。

回答by nite wolve

use this it worked for me

使用它对我有用

MIDDLEWARE_CLASSES = [
'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickHymaning.XFrameOptionsMiddleware',
]

回答by Dave Fontes

I had the same problem when switching my project from another pc. As i didn't like very much the option given to remove the

从另一台电脑切换我的项目时,我遇到了同样的问题。因为我不太喜欢给出的删除选项

'django.middleware.security.SecurityMiddleware'

'django.middleware.security.SecurityMiddleware'

I just created a new project and a new app, change the settings by hand and copy the rest of the files, and it worked :)

我刚刚创建了一个新项目和一个新应用程序,手动更改设置并复制其余文件,它起作用了:)

I'm guessing it has something to do with the secretkey of the project.

我猜这与项目的密钥有关。

回答by Papouche Guinslyzinho

If you previously used a virtualenvfor this project make sure that you activate it for the next time that you will use this project source bin/activateor check to see that you use the proper version of django.

如果您之前为此项目使用过virtualenv,请确保在下次使用该项目时激活它,source bin/activate或者检查您是否使用了正确版本的django.

If you want to removesome middlewares then you need to make sure of what you are doing.

如果你想删除一些中间件,那么你需要确定你在做什么。

回答by Juliana Skubs

(myvenv) pip uninstall django==(version)
(myenv) pip install django==(version)

Just it.

只是它。

回答by Hendrik T

I guess this happen in django version 1.7.x when you are trying to open project created using django version higher than 1.7.x

当您尝试打开使用高于 1.7.x 的 django 版本创建的项目时,我猜这会发生在 django 1.7.x 版中

removing 'django.middleware.security.SecurityMiddleware'helps but it create another problem 'session error'or something

删除有'django.middleware.security.SecurityMiddleware'帮助,但它会产生另一个问题'session error'或其他问题

to fix this:

解决这个问题:

pip uninstall django==1.7.x //your current version of django installed
pip install django==1.9.x //or version that is compatible with the project