2011-01-12 35 views
13

Tôi gặp sự cố khi tạo chế độ xem URL bằng django. Nó mang lại cho tôi lỗi này (Ferrol là một đối tượng Space):đối tượng django 'str' không thể gọi được

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 

Đây là mã:

gian/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 chính

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

spaces/urls.py

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

gian/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') 
+0

Urls.py chính của bạn trông như thế nào? Vấn đề có lẽ là ở đó. –

Trả lời

30

Trong không gian của bạn/urls.py nộp bạn phải cung cấp đường dẫn đầy đủ để xem phương pháp:

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

Hoặc như thế này:

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

Nó hiện đang hoạt động. Tôi quên để đặt các mô hình (apps.spaces.views) trước khi gọi các chức năng, xấu hổ về tôi! –

+1

@Oscar - nếu điều này trả lời câu hỏi của bạn - bạn có thể đánh dấu câu hỏi đó là được chấp nhận không? –

Các vấn đề liên quan