我刚开始使用django,只在网上书的第3章.
当我尝试访问该网站时,我继续遇到这个奇怪的错误.
/ test /'module'对象的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), )
我的hello功能很神秘.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")
Mark.. 16
你可能需要改变
from mysite import hello
喜欢的东西
from mysite.hello_file import hello_view
然后使用:
('^test/$',hello_view)
因为您需要传递(视图)函数,而不是文件或模块.我认为mgalgs试图解释,但我认为初学者有点不清楚.
你可能需要改变
from mysite import hello
喜欢的东西
from mysite.hello_file import hello_view
然后使用:
('^test/$',hello_view)
因为您需要传递(视图)函数,而不是文件或模块.我认为mgalgs试图解释,但我认为初学者有点不清楚.