在学习Django教程(本书是使用Django的Python Web开发)时,我一直遇到导入错误。
我在这里和Google一起寻找解决方案,但我找不到一个有效的解决方案。
我的错误是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
mysite的/博客/ urls.py
from django.conf.urls.defaults import patterns, include, url
from mysite.blog.views import archive
urlpatterns = patterns('',
url(r'^$', archive),
)
mysite的/博客/ 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')),
)
setting.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
答案 0 :(得分:1)
我认为你必须改变这一行:
url(r'^blog/', include('mysite.blog.urls')),
到此:
url(r'^blog/', include('blog.urls')),
希望它有所帮助!
答案 1 :(得分:0)
我的解决方案在这里:
mysite的/博客/ urls.py
from mysite.blog.views import archive
到
from blog.views import archive
mysite的/博客/ view.py
from mysite.blog.models import BlogPost
到
from blog.models import BlogPost
mysite的/ mysite的/ urls.py
url(r'^blog/', include('mysite.blog.urls')),
到
url(r'^blog/', include('blog.urls')),
我建议你在settings.py中设置DEBUG为true。当有错误时,它可以告诉你应用程序停止的地方。在这种情况下,你只需要跳到这个地方。调试会更好。
答案 2 :(得分:0)
将此文件更改为mysite / blog / urls.py
来自django.conf.urls.defaults导入模式,include,url
至
来自django.conf.urls导入模式,include,url
答案 3 :(得分:0)
我遇到了同样的问题,但我做的却与@Haimei做的相反
mysite的/博客/ urls.py 来自blog.views import archive将其更改为
from mysite.blog.views import archive
它有效。
还要确保删除.defaults 来自django.conf.urls.defaults导入模式,include,url