Django: AttributeError: 'str' object has no attribute 'resolve'

本文解决了一个常见的Django开发错误,即在浏览器加载页面时出现的str对象无resolve属性错误。问题源于在URL配置文件中错误地将url模式分配给了元组,而不是使用了正确的`django.conf.urls.defaults.patterns`函数。通过修正这一错误,可以避免此问题并确保项目的正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:http://redsymbol.net/articles/django-attributeerror-str-object-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:
Yuchan said...

Or, it happens when you incorrectly assign the tuple within the patterns function

say, (r'^hello/$' 'views.whatever') w/o the comma.

Arthur said...

:), thanks!

James Harrison Fisher said...

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.

Breen said...

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.

twneale said...

Thank God for this post. You are awesome.

Arthraim said...

If ROOT_URLCONF inside settings.py is not 'your_project.urls' may cause this error, too.
I spent half an hour for my careless....

rvause said...

This one got me completely stumped. Your saved me a lot of grief. Thankyou!

Aaron Maxwell said...

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.

Dave Everitt said...

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

Patrick Kimber said...

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'

Vadim said...

Also if
urlpatterns = patterns('', r'^People/$', 'mysite.People.views.index')
instead of
urlpatterns = patterns('', (r'^People/$', 'mysite.People.views.index'))

Jim Hefferon said...

I owe you an hour. Thank you.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值