没有名为index的模块

时间:2011-03-04 19:27:51

标签: python django

我收到错误     异常值:没有名为index 的模块。我在DjangoPhoneBook和电话簿文件夹中有__init__.py。我是django的新手,我正在关注djangoproject网站上的教程。我已经搜索了这个错误,但没有得到任何解决方案。

这个问题的原因和解决方案是什么?

Environment:

Request Method: GET
Request URL: http://localhost:8000/Book/
Django Version: 1.2.3
Python Version: 2.6.6
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'phonebook',
 'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
  91.                         request.path_info)
File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py" in resolve
  217.                     sub_match = pattern.resolve(new_path)
File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py" in resolve
  215.             for pattern in self.url_patterns:
File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py" in _get_url_patterns
  244.         patterns = getattr(self.urlconf_module, "urlpatterns",         self.urlconf_module)
File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py" in _get_urlconf_module
  239.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/pymodules/python2.6/django/utils/importlib.py" in import_module
  35.     __import__(name)

Exception Type: ImportError at /Book/
Exception Value: No module named index

urls.py的内容是

from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from DjangoPhoneBook.phonebook.views import *

urlpatterns = patterns('',
    (r'^Book/$', include('index'))
    (r'^admin/', include(admin.site.urls)),
)

和views.py是

  from django.http import HttpResponse
  def index(request):
     return HttpResponse("This is a view page")

1 个答案:

答案 0 :(得分:11)

从第一行中删除includeinclude是添加单独的url conf的语法,因此python正在寻找名为module的{​​{1}}。

将其更改为查看功能的完整python点路径。

index

编辑:我注意到您正在导入视图功能。您还可以指定视图函数本身

urlpatterns = patterns('',
    (r'^Book/$', 'path.to.my.views.index'), # <-- and add a comma here
    (r'^admin/', include(admin.site.urls)),
)