4

我是 django 新手,正在学习中。在做一些教程练习时,我遇到了一些需要帮助的错误。

错误:/blog 处的 ImportError。没有名为 urls 的模块

名为“blog”的应用程序下的 urls.py 文件是

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

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

名为'blog'的应用程序下的views.py文件是

# Create your views here.
from django.template import loader, Context
from django.http import HttpResponse
from mysite.blog.models import Blogpost

def archive(request):
    posts = BlogPost.objects.all()
    t = loader.get_template("archive.html")
    c = context({'posts': posts })
    return HttpResponse(t.render(c))

项目“mysite”下的 urls.py 文件是

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
url(r'^blog/', include('mysite.blog.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)

追溯:

File "D:\Super Developer\Python\lib\site-packages\django\core\handlers\base.py" in get_response
  89.                     response = middleware_method(request)
File "D:\Super Developer\Python\lib\site-packages\django\middleware\common.py" in process_request
  67.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in is_valid_path
  531.         resolve(path, urlconf)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in resolve
  420.     return get_resolver(urlconf).resolve(path)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in resolve
  298.             for pattern in self.url_patterns:
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in url_patterns
  328.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\Super Developer\Python\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
  323.             self._urlconf_module = import_module(self.urlconf_name)
File "D:\Super Developer\Python\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /blog
Exception Value: No module named urls

项目/应用程序的结构:

  1. 我的网站
    • 管理.py
    • 初始化.py
    • 设置.py
    • 网址.py
    • wsgi.py
    • 博客
      • 初始化.py
      • 模型.py
      • 视图.py
      • 网址.py
      • 测试.py
      • 模板 - archive.html

蟒蛇路径

['D:\Super Developer\Proj\mysite', 'C:\Windows\system32\python27.zip', 'D:\Super Developer\Python\DLLs', 'D:\Super Developer\Python\lib', 'D:\Super Developer\Python\lib\plat-win', 'D:\Super Developer\Python\lib\lib-tk', 'D:\Super Developer\Python', 'D:\Super Developer\Python \lib\site-packages']

4

8 回答 8

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

这可能需要更改为

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

注意:没有“mysite”前缀。

于 2012-06-25T08:41:31.663 回答
1

在博客的 urls.py 中,您导入

from django.conf.urls.defaults import patterns, include, url

但是在项目的 urls.py 中,你使用

from django.conf.urls import patterns, include, url

这是缩进的吗?后者似乎至少在我的环境中失败了。

于 2012-06-25T16:08:50.550 回答
1

检查您是否在应用程序 url 文件中使用了媒体或静态 url 声明

url(r'^media/(?P<path>.*)$', 'django.views.static.serve'
, {'document_root': settings.MEDIA_ROOT}
  ),

而不是在应用程序 url 文件中。

我遇到了同样的问题,换了这个,我就没有问题了。

于 2012-12-05T17:02:59.877 回答
0

A friend of mine was going through some django tutorial and got the same error. The mistake he made was in the file structures. In the new version of django, it creates two directory levels for example mysite/mysite and he tried putting the blog folder in mysite/blog instead of mysite/mysite/blog. After making that change, it all seemed to work well. Try to look at your structure and hope it helps.

于 2013-04-20T20:45:21.387 回答
0

我认为patterns最后一个元素后不能有逗号:

urlpatterns = patterns('',
    url(r'^$',archive), <-- delete this comma
)

也尝试运行manage.py shellimport blog.urls如果那里没有引发任何异常

于 2012-06-25T16:35:50.093 回答
0

也许您的博客文件夹中缺少__ init __.py文件。

于 2012-06-25T09:46:22.013 回答
0

看起来您使用的是 Django 1.6 或更高版本。由于框架中的一些更改,此代码将无法正常工作。在博客的 urls.py 中使用 1.5 或此代码:

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

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

这在views.py中:

    from django.template import loader, Context
    from django.http import HttpResponse
    from blog import models 

    def archive(request):
        posts = models.BlogPost.objects.all()
        t = loader.get_template('archive.html')
        c = Context({ 'posts': posts })
        return HttpResponse(t.render(c))
于 2014-07-09T11:47:25.747 回答
0

尝试给出这个:

url(r'^blog/', include('mysite.urls')),
于 2012-10-27T06:23:31.267 回答