我已经开始为Django 1.9更新一个小测试项目。我的结构如下:
- myproject
- settings.py
- urls.py
- wsgi.py
- __init__.py
- apps
- __init__.py
- myapp
- admin.py
- cms_app.py
- cms_appconfig.py
- models.py
- urls.py
- views.py
-__init__.py
我得到的错误是:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x1079c71e0>
Traceback (most recent call last):
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/myname/Documents/projects/myproject/myproject/urls.py", line 20, in <module>
url(r'^', include('cms.urls')), )
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 52, in include
urlconf_module = import_module(urlconf_module)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/cms/urls.py", line 19, in <module>
urlpatterns = get_app_patterns()
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/cms/appresolver.py", line 184, in get_app_patterns
return _get_app_patterns()
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/cms/appresolver.py", line 248, in _get_app_patterns
app_ns, get_patterns_for_title(path, title), app)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/cms/appresolver.py", line 174, in get_patterns_for_title
for pattern_list in get_app_urls(app.get_urls(title.page, title.language)):
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/site-packages/cms/appresolver.py", line 158, in get_app_urls
mod = import_module(urlconf)
File "/Users/myname/Documents/projects/myproject/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
Importerror: No module named 'myapp'
我添加了包含应用程序的网站后发生错误。 cms_app.py:
# coding=utf-8
from aldryn_apphooks_config.app_base import CMSConfigApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
from .models import MyappConfig
class MyApp(CMSConfigApp):
app_config = MyappConfig
app_name = 'myapp'
name = _("myapp")
urls = ['crowdfunding.urls']
apphook_pool.register(MyApp)
urls.py:
# coding=utf-8
from django.conf.urls import patterns, url
from .views import ProjectListView, ProjectDetailView
Urlpatterns = patterns(
'',
url(r'^$', ProjectListView.as_view(),
name="project_list"),
url(r'^(?P<slug>[^/]+)$',
ProjectDetailView.as_view(),
name="project_details"),
)
项目urls.py:
from __future__ import absolute_import, print_function, unicode_literals
from cms.sitemaps import CMSSitemap
from django.conf import settings
from django.conf.urls import * # NOQA
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = i18n_patterns(
'',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'^accounts/', include('allauth.urls')),
url(r'^myapp/', include('myproject.apps.myapp'), name="myapp"),
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^', include('cms.urls')), )
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns = patterns(
'',
url(r'^media/(?P<path>.*)$',
'django.views.static.serve', # NOQA
{'document_root': settings.MEDIA_ROOT,
'show_indexes': True
}), ) + staticfiles_urlpatterns() + urlpatterns # NOQA
我尝试过几个例子,例如直接导入项目urls.py中的视图,然后给我一个“apps”无法找到的错误,这对我来说是一个奇怪的错误。我还删除了项目urls.py中的“名称”,并查看了cms_app.py,如果没有正确给出应用程序名称(我认为这是正确的)。
settings.py包含我的项目:
INSTALLED_APPS = [
'djangocms_admin_style',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'django.contrib.sitemaps',
'django.contrib.staticfiles',
'django.contrib.messages',
...
'myproject',
'myproject.apps.myapp'
]
我之前没有看到过这个错误(在Django 1.8中),目前我在想,它与urls.py(s)和app加载有关。我确信这个应用程序在1.8中工作但是我找不到有关从1.8到1.9的更改的信息会产生这样的错误。所以我请你帮帮我,找到解决方案。
更新 我现在添加了一个/apps/myapp/apps.py(根据Django 1.9指南),内容如下:
# -*- coding: utf-8 -*-
from django.apps import AppConfig
class Myapp(AppConfig):
name = 'myapp'
verbose_name = 'My App Test'
并在apps / myapp / init .py:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
__version__ = '0.3'
default_app_config = 'myapp.apps.Myapp'
我还从主urls.py中删除了网址模式,并且也按照该指南的建议将其添加到apps / myapp / urls.py中。
现在错误信息有点短:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10975d7b8>
Traceback (most recent call last):
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 229, in wrapper
fn(*args, **kwargs)
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
autoreload.raise_last_exception()
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 252, in raise_last_exception
six.reraise(*_exception)
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 229, in wrapper
fn(*args, **kwargs)
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/site-packages/django/apps/config.py", line 86, in create
module = import_module(entry)
File "/Users/myname/Documents/projects/cr_new/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'myapp'
我认为,它与应用程序加载有关,但我无法弄清楚我做错了什么。