我不断收到错误消息,说没有这样的模块。

项目名称是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等。我显然在该行的结构上缺少一些超级简单的东西。

最佳答案

您的问题在这里:

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


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

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

您想要的是参考查看方法league

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


应该管用。

另外,请注意$之后的^league/,它表示URL模式的结尾。

关于python - View 中的Django导入错误-没有名为“x”的模块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18921141/

10-09 03:25