视图中的Django导入错误 - 没有名为'x'的模块

时间:2013-09-20 16:04:11

标签: python django python-2.7 django-views django-urls

我不断收到错误,表示没有这样的模块。

项目名称是gmblnew,我有两个子文件夹 - coregmblnew - 我正在处理的应用程序是核心。

我的 urls.py 文件是

from django.conf.urls import *

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

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

    url(r'^league/', include('core.views.league')),

    # 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)),
)

这似乎没问题。 views.py 文件为:

from django.http import HttpResponse

def league(request):
    from core.models import Division
    response = HttpResponse()
    response['mimetype'] = 'text/plain'
    response.write("<HTML><>BODY>\n")
    response.write("< TABLE BORDER=1><CAPTION>League List</CAPTION><TR>\n")
    all_leagues = Division.objects.all()
    for league in all_leagues:
        response.write("<TR>\n")
        response.write("<TD> %s" % league)
        response.write("</TD>\n")
    response.write("</BODY></HTML>")
    return response

回溯:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/chris/Dropbox/Django/gmblnew/gmblnew/urls.py" in <module>
  12.   url(r'^league/', include('core.views.league')),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
  25.         urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /admin/
Exception Value: No module named league

我在url(r'^league/', include('core.views.league')),行尝试了多种变体,包括gmblnew.core.views.leagueviews.leagueviews.view_league等。我显然遗漏了一些非常简单的内容那条线的结构。

2 个答案:

答案 0 :(得分:4)

你的问题在这里:

url(r'^league/', include('core.views.league')),

使用include指定一个不存在的模块。

include is used to include other url confs,而不是定位视图方法

您想要的是查看方法league

url(r'^league/$', 'core.views.league'),

应该有用。

另请注意$之后的^league/,它代表网址格式的 end

答案 1 :(得分:1)

include获取url文件的路径,而不是视图。只需写下来:

url(r'^league/', 'core.views.league'),