![](/img/trans.png)
[英]Django admin - 'NoneType' object has no attribute 'rindex'
[英]Django attribute error. 'module' object has no attribute 'rindex'
我剛剛開始使用 django,僅在在線書籍的第 3 章。
當我嘗試訪問該站點時,我不斷收到這個奇怪的錯誤。
/test/ 'module' object 處的 AttributeError 沒有屬性 '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),
)
我的你好 function 在我的網站上。 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 解釋器中執行 from 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'
你好 function 是
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)
因為你需要傳遞一個(視圖)function,而不是文件或模塊。 正如我認為 mgalgs 試圖解釋的那樣,但我認為對於初學者來說有點不清楚。
url 模式應該是字符串元組,而不是模塊。 就像是:
urlpatterns = patterns('',
('^test/$','hello.views.hello'),
)
編輯:您還可以將可調用對象傳遞給模式。 我以前從未見過這樣做過(文檔總是傳遞一個字符串)。 但是,您實際上是在傳遞一個模塊,而不是一個字符串或一個可調用的(因此 django 會感到困惑,首先將其視為必須是可調用的,因為它不是字符串,但隨后又嘗試將其視為字符串,因此對rindex
的調用)。 也許你打算像這樣傳遞hello.views.hello
:
urlpatterns = patterns('',
('^test/$',hello.views.hello),
)
或者,您可以將導入行更改為from mysite.hello.views import hello
,或者只使用字符串語法(我相信'hello.views.hello'
會按照最初的建議進行)。
通過以下方式添加基於 Class 的視圖時出現此錯誤:
url(r'^tv/$', views.MyTemplateView(), name='my_tv'),
當然應該添加.as_view()
來修復錯誤object has no attribute 'rindex'
:
url(r'^tv/$', views.MyTemplateView.as_view(), name='my_tv'),
嘗試這個
urlpatterns = patterns('',
('r^test/$','hello.views.hello'), )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.