[Fixed]-'str' object has no attribute 'resolve' when access admin site

30👍

According to excellent answer posted here:
http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/

There are generally several sources of this error:

  1. You missed ‘pattern keyword’:

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

    this should be changed to:

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

    Note that in Django 1.8+, it’s better to use a list of regexes instead of patterns.

    urlpatterns = [
        (r'^$', direct_to_template, {'template' : 'a.html'}),
        ...
    ]
    
  2. You missed a comma in some tuple, like:

    (r'^hello/$' 'views.whatever') 
    
  3. You commented out some url()s using triple-quotes

  4. 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'),
    
  5. You set ROOT_URLCONF to be a list

  6. When migrating from the pattern tuples to a regular list you forgot to remove the empty '' argument of the pattern.

Please check carefully if you don’t have one of this cases in your code.

7👍

For me, this caused the problem:

urlpatterns = patterns('',
    url(r'^all/$', 'album.views.albums'),
    """
    url(r'^create/$', 'album.views.create'),
    url(r'^get/(?P<album_id>\d+)/$', 'album.views.album'),
    url(r'^like/(?P<album_id>\d+)/$', 'album.views.like_album'),
"""
)

and this solved it:

urlpatterns = patterns('',
    url(r'^all/$', 'album.views.albums'),
)
"""
    url(r'^create/$', 'album.views.create'),
    url(r'^get/(?P<album_id>\d+)/$', 'album.views.album'),
    url(r'^like/(?P<album_id>\d+)/$', 'album.views.like_album'),
"""

I saw this possibility in a comment on http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/

Leave a comment