[Fixed]-Django attribute error. 'module' object has no attribute 'rindex'

16👍

You probably need to change

from mysite import hello

to something like

from mysite.hello_file import hello_view

And then use:

('^test/$',hello_view)

Because you need to pass a (view) function, not a file or module. As I think mgalgs was trying to explain, but I think a bit unclear for beginners.

👤Mark

5👍

I got this error when adding a Class Based View the following way:

url(r'^tv/$', views.MyTemplateView(), name='my_tv'),

of course .as_view() should be added to fix error object has no attribute 'rindex':

url(r'^tv/$', views.MyTemplateView.as_view(), name='my_tv'),
👤SaeX

4👍

The url pattern should be tuples of strings, not a module. Something like:

urlpatterns = patterns('',
      ('^test/$','hello.views.hello'), 
)

EDIT: You can also pass a callable to patterns. I simply had never seen it done that way before (the docs always pass a string). However, you’re actually passing a module, not a string or a callable (so django gets confused and first treats it like it must be a callable since it’s not a string but then goes back to trying to treat it like a string, hence the call to rindex). Maybe you meant to pass hello.views.hello like so:

urlpatterns = patterns('',
      ('^test/$',hello.views.hello), 
)

Alternatively, you could change your import line to from mysite.hello.views import hello, or just use the string syntax (I believe 'hello.views.hello' would do it as initially suggested).

👤mgalgs

0👍

Try this

urlpatterns = patterns('',      
    ('r^test/$','hello.views.hello'),  ) 

Leave a comment