我在将Django 1.6.2项目升级到Django 1.8.4时遇到了很多障碍。让我难过的最新问题就是这个问题。当我启动我的Django dev服务器并尝试访问我的网站时,我收到以下错误:
Exception Type: ImportError at /
Exception Value: No module named account.models
Traceback:
File "/Users/me/venv/myproj_dj_18/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
119. resolver_match = resolver.resolve(request.path_info)
File "/Users/me/venv/myproj_dj_18/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
366. for pattern in self.url_patterns:
File "/Users/me/venv/myproj_dj_18/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
402. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/me/venv/myproj_dj_18/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
396. self._urlconf_module = import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/www/myproj/conf/urls.py" in <module>
8. from apps.admin.views import all_admins
File "/www/myproj/apps/admin/views.py" in <module>
27. from apps.account.models import Account
这是我项目文件夹结构的一部分:
myproj
├── apps
│ ├── __init__.py
│ ├── account
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── forms.py
│ │ ├── models.py
│ │ ├── templates
│ │ ├── tests
│ │ ├── urls.py
│ │ └── views.py
│ ├── admin
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── forms.py
│ │ ├── models.py
│ │ ├── templates
│ │ ├── tests
│ │ ├── urls.py
│ │ └── views.py
├── conf
│ ├── __init__.py
│ ├── settings
│ │ ├── __init__.py
│ │ ├── base.py
│ ├── urls.py
│ ├── wsgi.py
├── manage.py
以下是我的网址文件的一部分:
# conf/urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib.auth.decorators import user_passes_test
from django.contrib import admin
from apps.admin.views import all_admins
urlpatterns = patterns('',
url(r'^account/', include('apps.account.urls')),
url(r'^members/', include('apps.members.urls')),
url(r'^admin/all_admins/$',
user_passes_test(lambda u: u.is_staff)(all_admins),
{'template': 'all_admins.html'},
name='all-admins'),
url(r'^admin/review/queue/$',
'apps.admin.views.review_queue',
{'template': 'review_queue.html'},
name='review-queue'),
url(r'^admin/', include(admin.site.urls)),
url(r'', include('apps.home.urls')),
)
这些是我已安装的应用:
# apps/conf/settings/base.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'apps.admin',
'apps.account',
'apps.home',
)
这是模型文件的一部分:
# apps/account/models.py
from django.db import models
class Account(models.Model):
pass
我按照文档继承了新的AppConfig类:
# apps/account/apps.py
from django.apps import AppConfig
class AccountConfig(AppConfig):
name = 'apps.account'
# apps/account/__init__.py
default_app_config = 'apps.account.apps.AccountConfig'
# apps/admin/apps.py
from django.apps import AppConfig
class MyAdminConfig(AppConfig):
name = 'apps.admin'
label = 'my_admin'
verbose_name = 'Administration'
# apps/admin/__init__.py
default_app_config = 'apps.admin.apps.MyAdminConfig'
我已经检查过以确保我的所有路径都是相互一致的:
$ python
>>> from django.apps import apps as myproj_apps
>>> account = myproj_apps.get_app_config('account')
>>> account.name # => 'apps.account'
>>> list(account.get_models()) # => [apps.account.models.Account]
我也可以做到这一点,没有错误:
$ python
>>> from apps.account.models import Account
>>> Account.objects.get(pk=1)
我尝试将上述所有路径从“apps.account.models”更改为“myproj.apps.account.models”,但这没有帮助。我还阅读了Django 1.7应用程序文档,但看不出我做错了什么。我的网络搜索中也没有出现任何问题。这看起来很简单。谁能看到我错过的东西?
谢谢!