django guardian importerror 但其他示例有效

django guardian importerror but other examples work

首先,我是 django 和 python 的新手。

我正在处理一个使用 django-guardian 1.3.2 的现有项目。我通过 pip list.

验证了部署此应用程序的服务器也使用 1.3.2

我正在尝试了解异常的原因。

现有 admin.py 个文件,可以正常加载。

#!/usr/bin/env python
# coding: utf-8
from guardian.admin import GuardedModelAdmin
from django.contrib import admin

from devices import models


class SomeModelAdmin(GuardedModelAdmin):
  pass


admin.site.register(models.SomeModel, SomeModelAdmin)

我添加了我的,这也适用于(在我的机器上)。

#!/usr/bin/env python
# coding: utf-8
from guardian.admin import GuardedModelAdmin


from django.contrib import admin
from common import models


class MyModel1Admin(GuardedModelAdmin):
    pass


admin.site.register(models.MyModel1, MyModel1Admin)


class MyModel2Admin(GuardedModelAdmin):
    pass


admin.site.register(models.MyModel2, MyModel2Admin)

我们部署了该应用程序,现在它无法启动并出现以下异常:

Traceback (most recent call last): File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 187, in call self.load_middleware() File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware mw_instance = mw_class() File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/middleware/locale.py", line 24, in init for url_pattern in get_resolver(None).url_patterns: File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/urlresolvers.py", line 365, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in urlconf_module self._urlconf_module = import_module(self.urlconf_name) File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module import(name) File "./sites/zpanel/urls.py", line 19, in admin.autodiscover() File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/contrib/admin/init.py", line 29, in autodiscover import_module('%s.admin' % app) File "/usr/local/share/.virtualenvs/my_app/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module import(name) File "./common/admin.py", line 3, in from guardian.admin import GuardedModelAdmin ImportError: No module named admin

服务器是运行python2.7.6,我用的是2.7.10。我认为这不是问题,因为其他 admin.py 文件从 guardina.admin 导入就好了。

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'sites.this_app',
    'notifications.email',
    'raven.contrib.django.raven_compat',
    'guardian',
    'my_stuff',
    'existing_ones'
    'rest_framework',
    'rest_framework.authtoken',
    'south',
    'djangular',
    'widget_tweaks',
    'djrill',
    'corsheaders',
    'django_extensions',
    'parler',
    'watson',
    'baldr',
)

事实证明,在与我的 admin.py 文件相同的目录中有一个名为 guardian 的目录。这个目录不在源代码中(不再),也不在我的本地机器上。我想这是很久以前部署的一些残留物...