我是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
项目/应用程序的结构:
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']
答案 0 :(得分:5)
url(r'^blog/', include('mysite.blog.urls')),
这可能需要更改为
url(r'^blog/', include('blog.urls')),
注意:没有'mysite'前缀。
答案 1 :(得分:1)
在博客的urls.py中,导入
from django.conf.urls.defaults import patterns, include, url
但是在项目的urls.py中,你使用
from django.conf.urls import patterns, include, url
这是缩进的吗?后者似乎至少在我的环境中失败了。
答案 2 :(得分:1)
检查您是否使用
在应用程序网址文件中对媒体或静态网址进行了声明url(r'^media/(?P<path>.*)$', 'django.views.static.serve'
, {'document_root': settings.MEDIA_ROOT}
),
而不是在应用程序url文件中。
我遇到了同样的问题,改变了这个问题,我不再有问题了。
答案 3 :(得分:0)
也许您错过了博客文件夹中的 __ init __。py 文件。
答案 4 :(得分:0)
我认为patterns
在最后一个元素之后不能有逗号:
urlpatterns = patterns('',
url(r'^$',archive), <-- delete this comma
)
还尝试运行manage.py shell
和import blog.urls
,如果没有在那里引发任何异常
答案 5 :(得分:0)
尝试给出:
url(r'^blog/', include('mysite.urls')),
答案 6 :(得分:0)
我的一个朋友正在阅读一些django教程并得到同样的错误。他犯的错误是在文件结构中。在新版本的django中,它创建了两个目录级别,例如mysite / mysite,他尝试将博客文件夹放在mysite / blog而不是mysite / mysite / blog中。在做出改变后,一切似乎都运转良好。试着看看你的结构并希望它有所帮助。
答案 7 :(得分: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))