sporadic writings from aaron maxwell

Tuesday, April 7, 2009

Django: AttributeError: 'str' object has no attribute 'resolve'

Here is today's obscure error message and its solution.

Say you are working on a Django project, using its development web server, and you get this exception when you try to load a page in the browser:

AttributeError: 'str' object has no attribute 'resolve'

It's because you forgot to type the word "patterns".

Specifically, in some url.py, you typed something like this:

urlpatterns = ('',
  (r'^$', direct_to_template, {'template' : 'a.html'}),
  # ...

when you should have typed this:

urlpatterns = patterns('',
  (r'^$', direct_to_template, {'template' : 'a.html'}),
  # ...

See the difference? In the first one, I'm incorrectly assigning urlpatterns to be a tuple. In the second, I'm correctly using the django.conf.urls.defaults.patterns function.

File this under "issues that cost me an annoying amount of time to solve, and which I'm documenting so it doesn't have to happen to anyone else."

6 comments:

Yuchan said...

Or, it happens when you incorrectly assign the tuple within the patterns function

say, (r'^hello/$' 'views.whatever') w/o the comma.

Arthur said...

:), thanks!

James Harrison Fisher said...

Also happened when, foolishly, I commented out some url()s using triple-quotes. The comment was not ignored; instead, it is passed as a string literal to the patterns() function.

Breen said...

Yeah I just spent 15 minutes staring at my urls.py, knowing there was something wrong in there somewhere, but I could not figure it out. Thank you for having this problem before me ;).

Writing a blog entry for this was a fantastic idea. Now the usual "google this exception" points to this seemingly unrelated fix. Template errors can be tricky to track the real issue.

twneale said...

Thank God for this post. You are awesome.

Arthraim said...

If ROOT_URLCONF inside settings.py is not 'your_project.urls' may cause this error, too.
I spent half an hour for my careless....