1

在学习 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 
4

4 回答 4

1

我认为你必须改变这一行:

url(r'^blog/', include('mysite.blog.urls')),

对此:

url(r'^blog/', include('blog.urls')),

希望能帮助到你!

于 2013-06-07T18:17:10.220 回答
0

我的解决方案在这里:

我的网站/博客/urls.py

from mysite.blog.views import archive 

from blog.views import archive

我的网站/博客/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 。当有错误时,它可以告诉你应用程序停止的地方。在这种情况下,你只需要跳到这个地方。最好调试一下。

于 2013-09-28T21:14:45.097 回答
0

将此文件 mysite/blog/urls.py 从 django.conf.urls.defaults import patterns, include, url
更改为 from django.conf.urls import patterns, include, url

于 2013-11-13T07:30:39.107 回答
0

我一直有同样的问题,但我所做的与@Haimei 所做的相反

mysite/blog/urls.py from blog.views import archive 将其更改为

from mysite.blog.views import archive

它奏效了。

还要确保 .defaults 从 django.conf.urls.defaults 导入模式中删除,包括,url

于 2014-01-26T00:33:37.063 回答