'str' object has no attribute 'resolve' when access admin site

后端 未结 2 994
独厮守ぢ
独厮守ぢ 2021-01-11 16:57

I keep getting this error: \'str\' object has no attribute \'resolve\'

when trying to accessing the django admin site and I can\'t figure out why. I have apps within

相关标签:
2条回答
  • 2021-01-11 17:19

    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.

    0 讨论(0)
  • 2021-01-11 17:37

    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/

    0 讨论(0)
提交回复
热议问题