python – Django屬性錯誤. ‘module’物件沒有屬性’rindex’
我剛剛開始使用django,只在網路書的第3章.
當我嘗試訪問該網站時,我不斷得到這個奇怪的錯誤.
/ test /
‘module’物件沒有屬性’rindex’
我的urls.py就是
from django.conf.urls.defaults import * from mysite import hello # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('', ('^test/$',hello), )
我的hello函式是mysite.
Python路徑是
['/home/james/django/mysite', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/home/james/django']
我真的不明白這裡發生了什麼.我假設我忽略了一些愚蠢的東西,因為它似乎很簡單.當我從python直譯器中的mysite import hello做的時候,它不會引起任何錯誤.
任何幫助都會很棒
編輯:
追溯
Environment: Request Method: GET Request URL: http://127.0.0.1:8000/test/ Django Version: 1.2.3 Python Version: 2.6.6 Installed Applications: ['django.contrib.auth', 'django.co ntrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages'] 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 123.return self.callback, args, kwargs File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py" in _get_callback 134.mod_name, func_name = get_mod_func(self._callback_str) File "/usr/lib/pymodules/python2.6/django/core/urlresolvers.py" in get_mod_func 78.dot = callback.rindex('.') Exception Type: AttributeError at /test/ Exception Value: 'module' object has no attribute 'rindex'
你好函式是
from django.http import HttpResponse def hello(request): return HttpResponse("Hello world")
你可能需要改變
from mysite import hello
像一樣
from mysite.hello_file import hello_view
然後使用:
('^test/$',hello_view)
因為你需要傳遞一個(view)函式,而不是一個檔案或者模組.正如我認為mgalgs試圖解釋的那樣,但是我覺得初學者有點不清楚.
http://stackoverflow.com/questions/7052578/django-attribute-error-module-object-has-no-attribute-rindex