2011-08-11 37 views
11

我不斷收到此錯誤: 「海峽」對象有沒有屬性「決心」「海峽」對象有沒有屬性「決心」當訪問管理站點

試圖訪問Django管理站點時

,我可以」不知道爲什麼 。 我有我的項目中有自己的admin.py文件的應用程序。 這可能導致它嗎? 這裏是我的urls.py:

from django.conf.urls.defaults import * 
import settings 
from django.contrib.auth.views import login, logout 
from views import index, simple, complex 
from django.views.generic.simple import direct_to_template 

# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 
urlpatterns = patterns('', 
    # Example: 
    # (r'^django_jchat/', include('django_jchat.foo.urls')), 
    # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
    # to INSTALLED_APPS to enable admin documentation: 
    (r'^admin/doc/', include('django.contrib.admindocs.urls')), 
    # Uncomment the next line to enable the admin: 
    (r'^admin/', include(admin.site.urls)), 

...

繼承人的追溯:

'str' object has no attribute 'resolve' 
Request Method:   GET 
Django Version:   1.3 
Exception Type:   AttributeError 
Exception Value: 
'str' object has no attribute 'resolve' 
Exception Location:  /home/dockedin/webapps/peebletalk/lib/python2.7/ 
django/core/urlresolvers.py in resolve, line 252 
Python Executable:  /usr/local/bin/python 
Python Version:   2.7.1 
Python Path: 
['/home/dockedin/webapps/peebletalk', 
'/home/dockedin/webapps/peebletalk/lib/python2.7', 
'/home/dockedin/lib/python2.7', 
'/usr/local/lib/python27.zip', 
'/usr/local/lib/python2.7', 
'/usr/local/lib/python2.7/plat-linux2', 
'/usr/local/lib/python2.7/lib-tk', 
'/usr/local/lib/python2.7/lib-old', 
'/usr/local/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/site-packages', 
'/usr/local/lib/python2.7/site-packages/PIL'] 
Server time: Wed, 10 Aug 2011 15:24:55 -0400 
Traceback Switch to copy-and-paste view 
    /home/dockedin/webapps/peebletalk/lib/python2.7/django/core/ 
handlers/base.py in get_response 
          response = middleware_method(request) 
     ... 
    ▶ Local vars 
    /home/dockedin/webapps/peebletalk/lib/python2.7/django/middleware/ 
common.py in process_request 
        if (not _is_valid_path(request.path_info, urlconf) 
and 
      ... 
    ▶ Local vars 
    /home/dockedin/webapps/peebletalk/lib/python2.7/django/middleware/ 
common.py in _is_valid_path 
       urlresolvers.resolve(path, urlconf) 

     ... 
    ▶ Local vars 
    /home/dockedin/webapps/peebletalk/lib/python2.7/django/core/ 
urlresolvers.py in resolve 
      return get_resolver(urlconf).resolve(path) 
      ... 
    ▶ Local vars 
    /home/dockedin/webapps/peebletalk/lib/python2.7/django/core/ 
urlresolvers.py in resolve 
      def resolve(self, path): 
       tried = [] 
       match = self.regex.search(path) 
       if match: 
        new_path = path[match.end():] 
        for pattern in self.url_patterns: 
         try: 
          sub_match = pattern.resolve(new_path) 
     ... 
         except Resolver404, e: 
          sub_tried = e.args[0].get('tried') 
          if sub_tried is not None: 
           tried.extend([[pattern] + t for t in 
sub_tried]) 
          else: 
           tried.append([pattern]) 

回答

27

根據出色答卷張貼在這裏: http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/

通常有這個錯誤的幾個來源:

  1. 你錯過了 '模式關鍵字':

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

    本應改爲:

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

    注意,在Django 1.8+,這是更好地使用正則表達式,而不是patterns的列表。

    urlpatterns = [ 
        (r'^$', direct_to_template, {'template' : 'a.html'}), 
        ... 
    ] 
    
  2. 你在一些元組錯過了一個逗號,如:

    (r'^hello/$' 'views.whatever') 
    
  3. 你的評論了一些網址()s,使用三引號

  4. 你不小心留下一個右括號中錯誤的地方:

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

    而不是:

    (r'^(?P\d{4})/$', 'archive_year', entry_info_dict, 'coltrane_entry_archive_year'), 
    
  5. 您設置ROOT_URLCONF是list

  6. 當從模式遷移元組,你忘了刪除模式的空''參數常規列表。

請仔細檢查,如果你沒有在你的代碼中的這種情況之一。

+0

,而不是僅僅鏈接到外部網站,你可能想在這裏實際包括一個答案。這意味着訪問者不必去其他任何地方,並且防止您的答案在該網站發生故障或消失時無效。 – Bart

+0

@Bart - 非常感謝,我還在學習,現在完成了。 – mnowotka

+2

偉大的答案,這治好了我的問題(由於三重引用列表中的一些項目) – RichVel

7

對於我來說,這引起了問題:

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'), 
""" 
) 

,這解決了它:

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'), 
""" 

我看到了這種可能性的評論http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/

相關問題