简体   繁体   中英

Django import error in views - no module named 'x'

I keep getting an error that there's no such module.

The project name is gmblnew, and I have two subfolders- core and gmblnew - the app I'm working on is core.

My urls.py file is

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

This seems to be fine. The views.py file is:

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

Traceback:

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

I've tried a number of variants on the url(r'^league/', include('core.views.league')), line, including gmblnew.core.views.league , views.league , views.view_league , etc. I'm obviously missing something super simple on the structure of that line.

Your problem is here:

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

By using include you are specifying a module, which does not exist.

include is used to include other url confs , and not to target view methods

What you want is refer to the view method league

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

should work.

Also, note the $ after ^league/ , which represents the end of the URL pattern.

include takes a path to a url file, not a view. Just write this instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM