Django-importorror在/博客。没有名为url的模块

2024-05-13 04:14:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

错误:ImportError at/blog。没有名为url的模块

名为“blog”的应用下的url.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”项目下的url.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. mysite网站
    • 管理.py
    • 初始.py
    • 设置.py
    • 网址.py
    • wsgi.py公司
    • 博客
      • 初始.py
      • 模型.py
      • 视图.py
      • 网址.py
      • 测试.py
      • 模板-archive.html

Python路径

['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']


Tags: djangoinfrompyimporturldeveloperadmin
3条回答

检查您是否使用

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

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

我有同样的问题,改变了这个,我不再有问题了。

在blog的url.py中,导入

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

但是在project的url.py中,您可以使用

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

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

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

这可能需要更改为

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

注意:没有“mysite”前缀。

相关问题 更多 >