django 'str' 对象不可调用 [英] django 'str' object is not callable

查看:21
本文介绍了django 'str' 对象不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 django 中创建 URL 视图时遇到问题.它给了我这个错误(ferrol 是一个 Space 对象):

I have a problem creating an URL view in django. It gives me this error (ferrol is a Space object):

TypeError at /spaces/ferrol/
'str' object is not callable
Request Method: GET
Request URL:    http://localhost:8000/spaces/ferrol/
Django Version: 1.2.3
Exception Type: TypeError
Exception Value:    
'str' object is not callable
Exception Location: /usr/local/lib/python2.6/dist-packages/Django-1.2.3-py2.6.egg/django/core/handlers/base.py in get_response, line 100

代码如下:

spaces/models.py

spaces/models.py

class Space(models.Model):

"""
Basic spaces model.
"""
name = models.CharField(_('Name'), max_length=100, unique=True)
description = models.TextField(_('Description'))
date = models.DateTimeField(auto_now_add=True)

logo = models.ImageField(upload_to='spaces/logos',
                         verbose_name=_('Logotype'))
banner = models.ImageField(upload_to='spaces/banners',
                           verbose_name=_('Banner'))

主 urls.py

urlpatterns = patterns('',

# Django administration
(r'^admin/', include(admin.site.urls)),

(r'^spaces/', include('apps.spaces.urls')),

(r'^static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': 'static'}),

)

if 'e_cidadania.apps.rosetta' in settings.INSTALLED_APPS:
urlpatterns += patterns('',
    url(r'^rosetta/', include('apps.rosetta.urls')),
)

空格/urls.py

urlpatterns = patterns('',
    # Spaces
    (r'^(?P<space_name>[-w./s]+)/', 'view_space_index'),
)

spaces/views.py

spaces/views.py

def view_space_index(request, space_name):

"""
Show the index page for the requested space.
"""
place = get_object_or_404(Space, name=space_name)

return object_detail(request,
                     queryset = Space.objects.all(),
                     object_id = place.id,
                     template_name = 'spaces/index.html',
                     template_object_name = 'get_place')

推荐答案

在你的 Spaces/urls.py 文件中,你必须提供查看方法的完整路径:

In your spaces/urls.py file you have to provide full path to view method:

urlpatterns = patterns('',
    # Spaces
    (r'^(?P<space_name>[-w./s]+)/', 'spaces.views.view_space_index'),
)

或者像这样:

urlpatterns = patterns('spaces.views',
    # Spaces
    (r'^(?P<space_name>[-w./s]+)/', 'view_space_index'),
)

这篇关于django 'str' 对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆