在学习 Django 教程(这本书是 Python Web Development with Django)时,我遇到了一个导入错误。
我已经在这里和谷歌寻找解决方案,但我找不到一个有效的解决方案。
我的错误是ImportError at /blog. No module named urls.
我正在使用django 1.5.1。
我的文件是结构 -
- mysite
manage.py
- blog
__init__.py
models.py
tests.py
urls.py
views.py
- templates
archive.html
- mysite
__init__.py
settings.py
urls.py
wsgi.py
我的网站/博客/urls.py
from django.conf.urls.defaults import patterns, include, url
from mysite.blog.views import archive
urlpatterns = patterns('',
url(r'^$', archive),
)
我的网站/博客/view.py
from django.template import loader, Context
from django.http import HttpResponse
from mysite.blog.models import BlogPost
def archive(requset):
posts = BlogPost.objects.all()
t = loader.get_template("archive.html")
c = context({ 'posts': posts })
return HttpResponse(t.render(c))
mysite/mysite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('mysite.blog.urls')),
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
)
设置.py
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'blog',
)
追溯
Traceback Switch to copy-and-paste view
/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response
resolver_match = resolver.resolve(request.path_info)
...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in resolve
for pattern in self.url_patterns:
...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py in import_module
__import__(name)
...
▶ Local vars
/home/han/Django/mysite/mysite/urls.py in <module>
url(r'^blog/', include('mysite.blog.urls')),
...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py in include
urlconf_module = import_module(urlconf_module)
...
▶ Local vars
/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py in import_module
__import__(name)
...
▶ Local vars