django 'str' 对象不可调用

标签 django django-views django-generic-views

我在 django 中创建 URL View 时遇到问题。它给了我这个错误(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

这是代码:

空间/模型.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'))

主要网址.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'),
)

空间/ View .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')

最佳答案

在您的空格/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'),
)

关于django 'str' 对象不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4668040/

相关文章:

python - Django 表单字段验证错误消息如 "Please fill out this field"

django - 我有一个 TemplateDoesNotExist 错误。我可以更新我的模型但不能删除它们?

python - 按高度/方向对图像进行排序

django - 需要帮助在通用 View 中获取 form_valid 的正确实例

django - 如何判断我是否正在使用模板中的 django 通用 View (CreateView 与 UpdateView)进行创建或更新?

django 网站和更改货币

python - Django - 链接到同一项目中不同应用程序的网址

javascript - 为什么我在vue中的数组不影响页面数据更新?

Django ajax 文件上传集成到模型表单

django - 使用 get_object_or_404 时无法解压不可迭代的 int obj