Django - 在/ blog的importError。 (没有名为urls的模块)

时间:2022-04-12 23:15:37

While studying Django tutorial (the book is Python Web Development with Django), I've been getting an import error.

在学习Django教程(本书是使用Django的Python Web开发)时,我遇到了导入错误。

I've looked for a solution on here and with Google, but I couldn't find one that worked.

我在这里和Google一起寻找解决方案,但我找不到一个有效的解决方案。

My error is ImportError at /blog. No module named urls.

我的错误是/ blog的ImportError。没有名为urls的模块。

I'm using django 1.5.1.

我正在使用django 1.5.1。

My files are structure -

我的文件是结构 -

 - 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/blog/urls.py

from django.conf.urls.defaults import patterns, include, url
from mysite.blog.views import archive

urlpatterns = patterns('',
        url(r'^$', archive),
)

mysite/blog/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

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 个解决方案

#1


1  

I think you have to change this line:

我想你必须改变这一行:

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

to this:

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

Hope it helps!

希望能帮助到你!

#2


0  

change this file mysite/blog/urls.py from django.conf.urls.defaults import patterns, include, url
to from django.conf.urls import patterns, include, url

从django.conf.urls.defaults导入模式更改此文件mysite / blog / urls.py,包含,来自django.conf.urls的url导入模式,include,url

#3


0  

I have been having the same problem but I did just the opposite of what @Haimei did

我遇到了同样的问题,但我的确与@Haimei的做法恰恰相反

mysite/blog/urls.py from blog.views import archive changed it to

来自blog.views import archive的mysite / blog / urls.py将其更改为

from mysite.blog.views import archive

and it worked.

它工作。

also make sure that .defaults is removed from from django.conf.urls.defaults import patterns, include, url

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

#4


0  

My solution is here:

我的解决方案在这里:

mysite/blog/urls.py

from mysite.blog.views import archive 

to

from blog.views import archive

mysite/blog/view.py

from mysite.blog.models import BlogPost 

to

from blog.models import BlogPost

mysite/mysite/urls.py

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

to

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

Here I suggest you to set DEBUG true in settings.py . when there is mistake, it can tell you the place where the application stop. In this case, you just need to jump to this place. It would be better to debug.

在这里,我建议你在settings.py中设置DEBUG为true。当有错误时,它可以告诉你应用程序停止的地方。在这种情况下,你只需要跳到这个地方。调试会更好。

#1


1  

I think you have to change this line:

我想你必须改变这一行:

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

to this:

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

Hope it helps!

希望能帮助到你!

#2


0  

change this file mysite/blog/urls.py from django.conf.urls.defaults import patterns, include, url
to from django.conf.urls import patterns, include, url

从django.conf.urls.defaults导入模式更改此文件mysite / blog / urls.py,包含,来自django.conf.urls的url导入模式,include,url

#3


0  

I have been having the same problem but I did just the opposite of what @Haimei did

我遇到了同样的问题,但我的确与@Haimei的做法恰恰相反

mysite/blog/urls.py from blog.views import archive changed it to

来自blog.views import archive的mysite / blog / urls.py将其更改为

from mysite.blog.views import archive

and it worked.

它工作。

also make sure that .defaults is removed from from django.conf.urls.defaults import patterns, include, url

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

#4


0  

My solution is here:

我的解决方案在这里:

mysite/blog/urls.py

from mysite.blog.views import archive 

to

from blog.views import archive

mysite/blog/view.py

from mysite.blog.models import BlogPost 

to

from blog.models import BlogPost

mysite/mysite/urls.py

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

to

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

Here I suggest you to set DEBUG true in settings.py . when there is mistake, it can tell you the place where the application stop. In this case, you just need to jump to this place. It would be better to debug.

在这里,我建议你在settings.py中设置DEBUG为true。当有错误时,它可以告诉你应用程序停止的地方。在这种情况下,你只需要跳到这个地方。调试会更好。