aaron maxwell

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."


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....

rvause said...

This one got me completely stumped. Your saved me a lot of grief. Thankyou!

Aaron Maxwell said...

Another thing that can cause this error: if you place have a comma at the end of the definition of urlpatterns, so that instead of being a tuple of patterns, urlpatterns is a tuple of ONE TUPLE of patterns. In other words, you meant to type this:

urlpatterns = patterns('',
# some patterns here

... but instead you typed this:

urlpatterns = patterns('',
# some patterns here

Note the trailing comma.

Dave Everitt said...

Or (adding my own dumbness to the above causes) when you're tired and:

...(following James Bennett's Practical Django Projects) you (mis)type 'detal' instead of 'detail':

def get_absolute_url(self):
return ('coltrane_entry_detal', (), {

...you carelessly leave a closing bracket in the wrong place:

(r'^(?P\d{4})/$', 'archive_year', entry_info_dict), 'coltrane_entry_archive_year',

instead of:

(r'^(?P\d{4})/$', 'archive_year', entry_info_dict, 'coltrane_entry_archive_year'),

Patrick Kimber said...

I had the same error when I set ROOT_URLCONF to be a list e.g:

ROOT_URLCONF = ['site_shop.urls']

... it should have been a simple string i.e:

ROOT_URLCONF = 'site_shop.urls'

Vadim said...

Also if
urlpatterns = patterns('', r'^People/$', 'mysite.People.views.index')
instead of
urlpatterns = patterns('', (r'^People/$', 'mysite.People.views.index'))

Jim Hefferon said...

I owe you an hour. Thank you.