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."
Comments:
Or, it happens when you incorrectly assign the tuple within the patterns function
say, (r'^hello/$' 'views.whatever') w/o the comma.
:), thanks!
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.
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.
Thank God for this post. You are awesome.
If ROOT_URLCONF inside settings.py is not 'your_project.urls' may cause this error, too.
I spent half an hour for my careless....
This one got me completely stumped. Your saved me a lot of grief. Thankyou!
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.
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'),
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'
Also if
urlpatterns = patterns('', r'^People/$', 'mysite.People.views.index')
instead of
urlpatterns = patterns('', (r'^People/$', 'mysite.People.views.index'))
I owe you an hour. Thank you.