1

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

项目名称是 gmblnew,我有两个子文件夹-core而且gmblnew- 我正在开发的应用程序是核心。

我的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.league, views.league,views.view_league等。我显然在该行的结构上遗漏了一些超级简单的东西。

4

2 回答 2

3

你的问题在这里:

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

通过使用include,您正在指定一个不存在的模块。

include用于包含其他 url confs,而不是针对视图方法

你想要的是参考视图方法league

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

应该管用。

另外,请注意$after ^league/,它表示 URL 模式的结束

于 2013-09-20T16:10:32.790 回答
0

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

url(r'^league/', 'core.views.league'),
于 2013-09-20T16:10:52.013 回答