[英]Django - ImportError at /blog. No module named urls
我是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 \\站點包]
url(r'^blog/', include('mysite.blog.urls')),
這可能需要更改為
url(r'^blog/', include('blog.urls')),
注意:沒有'mysite'前綴。
在博客的urls.py中,導入
from django.conf.urls.defaults import patterns, include, url
但是在項目的urls.py中,你使用了
from django.conf.urls import patterns, include, url
這是縮進的嗎? 后者似乎至少在我的環境中失敗了。
檢查您是否使用應用程序URL文件中的媒體或靜態URL進行了聲明
url(r'^media/(?P<path>.*)$', 'django.views.static.serve'
, {'document_root': settings.MEDIA_ROOT}
),
而不是在應用程序url文件中。
我遇到了同樣的問題,改變了這個問題,我不再有問題了。
也許您在博客文件夾中缺少__ init __.py文件。
我認為patterns
在最后一個元素之后不能有逗號:
urlpatterns = patterns('',
url(r'^$',archive), <-- delete this comma
)
如果沒有在那里引發任何異常,也嘗試運行manage.py shell
並import blog.urls
試試這個:
url(r'^blog/', include('mysite.urls')),
我的一個朋友正在閱讀一些django教程並得到了同樣的錯誤。 他犯的錯誤是在文件結構中。 在新版本的django中,它創建了兩個目錄級別,例如mysite / mysite,他嘗試將博客文件夾放在mysite / blog而不是mysite / mysite / blog中。 在做出改變后,一切似乎都運轉良好。 試着看看你的結構並希望它有所幫助。
看起來你正在使用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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.