使用django框架快速入门键入错误

时间:2014-01-13 15:10:50

标签: django django-rest-framework

我在尝试遵循Django Rest Framework的快速启动示例时遇到错误。据我所知,我已根据需要设置了所有内容,我已经过了几次,但似乎无法找到错误 错误如下

    Environment:


    Request Method: GET
    Request URL: http://webdev.lab.unlab.ca/

    Django Version: 1.6
    Python Version: 2.7.3
    Installed Applications:
    ('django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'rest_framework')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware')


    Traceback:
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
      114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
      69.             return self.dispatch(request, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
      57.         return view_func(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
      399.             response = self.handle_exception(exc)
    File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
      387.             self.initial(request, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in initial
      317.         self.check_permissions(request)
    File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in check_permissions
      273.         for permission in self.get_permissions():
    File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in get_permissions
      226.         return [permission() for permission in self.permission_classes]

    Exception Type: TypeError at /
    Exception Value: 'type' object is not iterable
谁知道发生了什么事?

2 个答案:

答案 0 :(得分:17)

permission_classes中的View/ViewSet必须是元组

你写了这样的话:

permission_classes = (permissions.IsAuthenticated)

忘记逗号。

尝试:

 permission_classes = (permissions.IsAuthenticated,)

答案 1 :(得分:2)

变量

self.permission_classes

在该行:

return [permission() for permission in self.permission_classes]

不会返回列表。因此,您的服务器无法循环它,即“不可迭代”。