django 'str'对象不可调用

发布于 2024-10-11 07:20:14 字数 1972 浏览 2 评论 0原文

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

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

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

Main urls.pyspaces

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.pyspaces

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

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

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

Here is the code:

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

Main urls.py

urlpatterns = patterns('',

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

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

(r'^static/(?P<path>.*)

spaces/urls.py

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

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

spaces/urls.py

spaces/views.py

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-10-18 07:20:14

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

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

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

Or like this:

urlpatterns = patterns('spaces.views',
    # Spaces
    (r'^(?P<space_name>[-\w\./\s]+)/', 'view_space_index'),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文