关于python 2.7:Django ImportError:无法导入名称get_permission_codename

Django ImportError: cannot import name get_permission_codename

我正在用django教程做探戈。 我是关于使用模型的第5章,我正在设置管理员网站。 我得到这个奇怪的错误:

ImportError: cannot import name get_permission_codename

当我删除时,这似乎消失了

1
admin.autodiscover()

来自我的project / urls.py。 但是我担心我将需要这样做。

这是我运行开发服务器时获得的Traceback:

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/admin/

Django Version: 1.5.4 Python Version: 2.7.3 Installed Applications:
('django.contrib.auth', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sites',
'django.contrib.messages', 'django.contrib.staticfiles',
'django.contrib.admin', 'rango') Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')

Traceback: File
"/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py"
in get_response
103. resolver_match = resolver.resolve(request.path_info) File
"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py"
in resolve
319. for pattern in self.url_patterns: File"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py"
in url_patterns
347. patterns = getattr(self.urlconf_module,"urlpatterns", self.urlconf_module) File
"/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py"
in urlconf_module
342. self._urlconf_module = import_module(self.urlconf_name) File
"/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in
import_module
35. import(name) File"/home/gpanterov/MyProjects/django/tango_with_django_project/tango_with_django_project/urls.py"
in
6. admin.autodiscover() File"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/init.py"
in autodiscover
29. import_module('%s.admin' % app) File"/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in
import_module
35. import(name) File"/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/admin.py"
in
5. from django.contrib.admin.checks import InlineModelAdminChecks File
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/checks.py"
in
6. from django.contrib.admin.utils import get_fields_from_path, NotRelationField, flatten File
"/usr/local/lib/python2.7/dist-packages/django/contrib/admin/utils.py"
in
6. from django.contrib.auth import get_permission_codename

Exception Type: ImportError at /admin/ Exception Value: cannot import
name get_permission_codename

当我删除admin.autodiscover()行时,开发服务器运行并且我能够进入管理面板,但是当我使用我之前创建的超级用户密码登录时,我收到消息"您没有权限 编辑任何东西。" 而且我没有看到我创建的任何类别。


将django从最新版本(1.8)降级到旧版本(1.4)进行测试时出现此错误。

这里的问题是git在切换分支时不会删除.pyc文件(因为它们是.gitignore'ed),而python只在相应的.py文件比.pyc文件更新时才重新生成它们(请参阅此内容) 问题详情)。

解决方案是删除django/contrib/admindjango/contrib/contenttypes目录中的所有*.pyc文件。


无法导入名称get_permission_codename

确保您没有在系统中安装所有版本的django。
如果有然后删除所有并安装新鲜的django