当访问管理站点时,'str'对象没有属性'resolve' [英] 'str' object has no attribute 'resolve' when access admin site
问题描述
我不断得到这个错误:
'str'对象没有属性'resolve'
I keep getting this error: 'str' object has no attribute 'resolve'
当尝试访问django管理站点,我可以'找出
为什么。
我的项目中有应用程序拥有自己的admin.py文件。
可以这样吗?
这里是我的urls.py:
when trying to accessing the django admin site and I can't figure out why. I have apps within my project that have their own admin.py files. Could this cause it? here is my 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])
推荐答案
根据这里发布的优秀答案:
http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/
According to excellent answer posted here: http://redsymbol.net/articles/django-attributeerror-str-object-no-attribute-resolve/
通常有几个错误来源:
-
您错过了'模式关键字':
You missed 'pattern keyword':
urlpatterns = ('',
(r'^$', direct_to_template, {'template' : 'a.html'}),
# ...
应该更改为: / p>
this should be changed to:
urlpatterns = patterns('',
(r'^$', direct_to_template, {'template' : 'a.html'}),
# ...
请注意,在Django 1.8+最好使用正则表达式列表,而不是模式
。
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'}),
...
]
您在某些元组中遗漏了逗号,如:
You missed a comma in some tuple, like:
(r'^hello/$' 'views.whatever')
你评论了一些url() -quotes
You commented out some url()s using triple-quotes
你不小心在错误的地方留下一个括号:
You carelessly leave a closing bracket in the wrong place:
(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'),
您将 ROOT_URLCONF
设置为列表
从模式元组迁移到常规列表时忘了删除模式的空的''
参数。
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.
这篇关于当访问管理站点时,'str'对象没有属性'resolve'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!